2019年5月22日 星期三

Kotlin 高中程式練習題 a002: 簡易加法 請寫一個程式,讀入兩個數字,並求出它們的和。

Kotlin 高中程式練習題 a002: 簡易加法 

請寫一個程式,讀入兩個數字,並求出它們的和。
輸入說明
每一組輸入有兩個整數,絕對值皆小於 106
輸出說明
對於每組輸入,輸出該兩整數的和。


fun main(args: Array<String>) {
    var t=1
    println("Each set of input has two integers, separated by space")
    println("the end, enter 3 blanks")
   
    do {
        val (a , b) = readLine()!!.split(' ')
        if (parseInt(a) == null)
            {
                t=0
                println("........")
            }   
        else if (parseInt(b) == null)
            {
                t=0
                println("........")
            }   
        else
            {
                val a1=a.toInt()
                val b1=b.toInt()
                val sum=a1+b1
                println("The sum of $a1 & $b1 = $sum")
            }   
    } while (t==1)
}   

fun parseInt(str: String): Int? {
    return str.toIntOrNull()
}


輸出畫面
Each set of input has two integers, separated by space
the end, enter 3 blanks 結束按空白三次
12 34
The sum of 12 & 34 = 46
34 67
The sum of 34 & 67 = 101
 
   ........

沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...