美文网首页
3、随机数

3、随机数

作者: 北方素素 | 来源:发表于2018-06-24 15:12 被阅读0次

今天的课程主要将怎样在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
   }
}

相关文章

  • 系统随机数产生方法

    1、通过系统环境变量($RANDOM)产生随机数 2、通过openssl产生随机数 3、通过时间获得随机数(dat...

  • iOS-OpenCV之随机数

    1.随机数发生器类RNG 2.随机数范围确定 rng.uniform(a,b) 3.使用随机数画图 4.效果展示

  • Project2-GameplayKit, layer

    1.利用GameplayKit来为随机数组中的内容 2.利用GameplayKit产生随机数 3.利用layer给...

  • Python numpy学习笔记之生成随机数

    废话不多说,直接开干。 生成均匀离散的随机数 生成服从均匀分布、3行2列的随机数 生成服从正态分布的随机数 经典例...

  • java 获取随机数

    java产生随机数的3种方式 一.使用Math.random()方法 Math.random() 产生的随机数是0...

  • 高并发分布式无碰撞ID生成机制

    转载请注明出处:奇思漫想 由于用户管理(APP后端的订单号生成机制是采用 机构编号+时间戳+3位随机数+3位随机数...

  • 3、随机数

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

  • js简易抽奖盘制作

    原理1、利用 Math.floor获取随机数字;2、点击按钮后,获取随机数字存为变量;3、设置循环定时器依次修改背...

  • 密码学基础之伪随机数

    随机数分类 真随机数 伪随机数2.1 强伪随机数2.2 弱伪随机数 真随机数:其定义为随机样本不可重现。实际上只要...

  • 在以太坊生成随机数的几种方式(含代码)

    一、什么是随机数 随机数都是由随机数生成器(Random Number Generator)生成的。随机数分为”真...

网友评论

      本文标题:3、随机数

      本文链接:https://www.haomeiwen.com/subject/bpajyftx.html