如何创建一个避免用户编写字符串的随机函数

时间:2018-09-24 16:06:59

标签: java kotlin

我想创建一个函数,允许用户输入2个数字(fromto) 但我不希望他们使from大于to并且不希望他们输入字符串(我尝试过(如果是(来自字符串),但它不起作用)

fun rand() {

    println("Enter First number: ")
    var first = readLine()!!.toInt()
    println("Enter second number")
    val second = readLine()!!.toInt()
    if(second - first < 0) {
        do {
            println("You have to make sure that your first number is larger than the second")
            println("Enter your First number: ")
            val first = readLine()!!.toInt()
            print("Please enter a number smaller than the first number")
            val second: Int = readLine()!!.toInt()
        } while(first - second < 0)

    }
    val random = Random().nextInt(second - first) + first
    println("Your number is $random")
}

0 个答案:

没有答案