2019年5月22日 星期三

Kotlin 高中程式練習題 a003: 兩光法師占卜術

Kotlin 高中程式練習題 a003: 兩光法師占卜術

兩光法師時常替人占卜,由於他算得又快有便宜,因此生意源源不絕,時常大排長龍,他想算 得更快一點,因此找了你這位電腦高手幫他用電腦來加快算命的速度。
  他的占卜規則很簡單,規則是這樣的,輸入一個日期,然後依照下面的公式:
M=月
D=日 
S=(M*2+D)%3
得到 S 的值,再依照 S 的值從 0 到 2 分別給與 普通、吉、大吉 等三種不同的運勢


fun main(args: Array<String>) {
    var t=1
    println("Each set of input has two integers, separated by space")
    println("first is Month 1..12 , second is Day 1..31 , 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 if (a.toInt() !in 1..12)
            {
                t=0
                println("Month 1..12")
            }
        else if (b.toInt() !in 1..31)
            {
                t=0
                println("Day 1..31")
            }
        else    
            {
                val a1=a.toInt()
                val b1=b.toInt()
                val sum=(a1*2+b1)%3
                println("S=(M*2+D)%3= $sum")
                if (sum==0)
                    println("Normal fortune")
                else if (sum==1)
                    println("Luck Fortune")
                else    
                    println("Good luck Fortune")
            }    
    } while (t==1)
}    

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


輸出畫面
Each set of input has two integers, separated by space
first is Month 1..12 , second is Day 1..31 , the end, enter 3 blanks
2 3
S=(M*2+D)%3= 1
Luck Fortune
1 1
S=(M*2+D)%3= 0
Normal fortune
12 4
S=(M*2+D)%3= 1
Luck Fortune

沒有留言:

張貼留言

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

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