3、随机数

今天的课程主要将怎样在Kotlin中使用随机数:
首先复习一下上一期的内容,写一个函数:

fun feedTheFish(){
    val day = "Tuesday"
    val food = "pellets"
    println("Today is $day and the fish eat $food")
}

之后再写一个:

fun randomDay():String{
    val week = listOf("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
    return week[Random().nextInt(7)]
}

其中,Random().nextInt(7)生成一个从0到6(不包含7)的随机数字
接下来又是练习题:

  1. Create a main() function.
  2. From the main() function, call a function, getFortuneCookie(), that returns a String.
  3. Create a getFortuneCookie() function that takes no arguments and returns a String.
  4. In the body of getFortuneCookie(), create a list of fortunes. Here are some ideas:
    "You will have a great day!"
    "Things will go well for you today."
    "Enjoy a wonderful day of success."
    "Be humble and all will turn out well."
    "Today is a good day for exercising restraint."
    "Take it easy and enjoy life!"
    "Treasure your friends because they are your greatest fortune."
  5. Below the list, print: "Enter your birthday: "
    Hint: Use print(), not println()
  6. Create a variable, birthday.
  7. Read the user's input form the standard input and assign it to birthday. If there is no valid input, set birthday to 1.
    Hint: Use readLine() to read a line of input (completed with Enter) as a String.
    Hint: In Kotlin, you can use toIntOrNull() to convert a number as a String to an Integer numeric. If the user enters "", toIntOrNull returns null.
    Hint: Check for null using the ? operator and use the ?: operator to handle the null case.
  8. Divide the birthday by the number of fortunes, and use the remainder as the index for the fortune to return.
  9. Return the fortune.
  10. In main(), print: "Your fortune is: ", followed by the fortune string.

还有额外练习:
Use a for loop to run the program 10 times, or until the "Take it easy" fortune has been selected.

标准答案是:

fun main(args: Array<String>) {
   println("\nYour fortune is: ${getFortune()}")
}

fun getFortune() : String {
   val fortunes = listOf( "You will have a great day!",
      "Things will go well for you today.",
      "Enjoy a wonderful day of success.",
      "Be humble and all will turn out well.",
      "Today is a good day for exercising restraint.",
      "Take it easy and enjoy life!",
      "Treasure your friends, because they are your greatest fortune.")
   print("\nEnter your birthday: ")
   val birthday = readLine()?.toIntOrNull() ?: 1
   return fortunes[birthday.rem(fortunes.size)]
}

扩展练习的答案是:

fun main(args: Array<String>) {
   var fortune: String
   for (i in 1..10) {
      fortune = getFortune()
      println("\nYour fortune is: $fortune")
      if (fortune.contains("Take it easy")) break
   }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,436评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,248评论 3 20
  • 在我所认识的朋友里,唯一能使文科理科,男生女生通通买账的国家不外乎两个,一个是美帝,另一个是德国,原因无需多言,不...
    竹影M阅读 3,569评论 1 7
  • 23岁的年纪,在大三暑假要决定未来毕业是考研和择业上做出来我自己的选择。 现在的我很着急,迫切想要拥有那些虚荣的东...
    学生阿豪阅读 199评论 0 0
  • 俗话说得好“巧妇难为无米之炊”,我们想要学好英语,就要学会善用资源,是不是常会觉得不知道从哪里开始学习,不知道怎么...
    颖子Mandy阅读 2,487评论 37 141