Kotlin 二分法算法游戏--猜价格

本人最新想学习算法,算法是提高程序性能的关键!
程序就是数据结构和算法!

写了一个二分法的游戏,供大家参考:
当然,语言基于kotlin

import java.util.*

/**
 * Created by Administrator on 2017/10/18.
 */
fun main(args: Array<String>) {
//    println("请输入商品真实价格")
//    val scanner = Scanner(System.`in`)
//    val realPrice = scanner.nextInt()
//    Runtime.getRuntime().exec("cmd cls")
    println("请用户猜测商品价格")
    val realPrice = Random().nextInt(109999)
    var guessPrice = Scanner(System.`in`).nextInt()

    while (guessPrice != realPrice){
        if (guessPrice > realPrice){
            println("猜大了")
        }else{
            println("猜小了")
        }
        guessPrice = Scanner(System.`in`).nextInt()
    }
    println("恭喜你猜对了")





}
posted @ 2017-10-19 00:16  有点理想的码农  阅读(266)  评论(0编辑  收藏  举报