Kotlin程式生成乘法表
在本文中,我們將瞭解如何列印乘法表。乘法表是透過使用for迴圈迭代所需的輸入10次,並在每次迭代中將輸入值乘以1到10的數字來建立的。
以下是相同的演示 -
假設我們的輸入是 -
Input : 16
期望的輸出將是 -
The multiplication table of 16 is : 16 * 1 = 16 16 * 2 = 32 16 * 3 = 48 16 * 4 = 64 16 * 5 = 80 16 * 6 = 96 16 * 7 = 112 16 * 8 = 128 16 * 9 = 144 16 * 10 = 160
演算法
步驟1 - 開始
步驟2 - 宣告兩個整數值,分別命名為myInput和i。
步驟3 - 定義值
步驟4 - 使用for迴圈迭代1到10,在每次迭代中,將1到10的數字與輸入相乘。
步驟5 - 在每次迭代後顯示結果值。
步驟6 - 停止
示例1
在這個例子中,我們將使用for迴圈生成乘法表。首先,宣告並初始化要生成乘法表的輸入 -
val myInput = 16
現在,使用for迴圈從1到10,因為我們想要顯示一個表 -
for (i in 1..10) { val product = myInput * i println("$myInput * $i = $product") }
現在讓我們看看使用for迴圈生成乘法表的完整示例 -
fun main() { val myInput = 16 println("The input is defined as $myInput") println("
The multiplication table of $myInput") for (i in 1..10) { val product = myInput * i println("$myInput * $i = $product") } }
輸出
The input is defined as 16 The multiplication table of 16 16 * 1 = 16 16 * 2 = 32 16 * 3 = 48 16 * 4 = 64 16 * 5 = 80 16 * 6 = 96 16 * 7 = 112 16 * 8 = 128 16 * 9 = 144 16 * 10 = 160
示例2
在這個例子中,我們將生成乘法表 -
fun main() { val myInput = 16 println("The input is defined as $myInput") multiplicationTable(myInput) } fun multiplicationTable(myInput: Int) { println("
The multiplication table of $myInput") for (i in 1..10) { val product = myInput * i println("$myInput * $i = $product") } }
輸出
The input is defined as 16 The multiplication table of 16 16 * 1 = 16 16 * 2 = 32 16 * 3 = 48 16 * 4 = 64 16 * 5 = 80 16 * 6 = 96 16 * 7 = 112 16 * 8 = 128 16 * 9 = 144 16 * 10 = 160
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP