Scala小游戏--猜拳(基于scala的人机猜拳游戏)

                                                 任务描述

完成人机猜拳互动游戏的开发,电脑通过程序中的随机数实现出拳。代码如下图1-1所示。

import scala.util.Random
//在类型中匹配如果case中有string
object Caquangame {
  def main(args: Array[String]): Unit = {
    val arr=Array("拳头","剪刀","布")
    val diannao=arr(Random.nextInt(arr.length))
    val ren=arr(Random.nextInt(arr.length))

    if (diannao.equals("拳头")){
      if(ren.equals("布")){
        println("电脑:"+diannao+"," +"我:"+ren+",您赢了^_^")
      }
      if(ren.equals("剪刀")) {
        println("电脑:" + diannao +"," +"我:"+ren+ ",您输了^_^")
      }
      if(ren.equals("拳头")) {
        println("电脑:" + diannao +"," + "我:"+ren+",平局^_^")
      }
      }
    if (diannao.equals("剪刀")){
      if(ren.equals("布")){
        println("电脑:"+diannao+"," +"我:"+ren+",您输了^_^")
      }
      if(ren.equals("拳头")) {
        println("电脑:" + diannao + "," +"我:"+ren+",您赢了^_^^_^")
      }
      if(ren.equals("剪刀")) {
        println("电脑:" + diannao+"," + "我:"+ren+",平局^_^")
      }

    }
    if (diannao.equals("布")){
      if(ren.equals("布")){
        println("电脑:" + diannao +","+"我:"+ren+"平局^_^")
      }
      if(ren.equals("剪刀")) {
        println("电脑:" + diannao+","+"我:"+ren+ ",您赢了^_^")
      }
      if(ren.equals("拳头")) {
        println("电脑:" + diannao +","+"我:"+ren+ ",您输了^_^^_^")
      }
    }

    }


}

结果如下:

电脑:布,我:布平局^_^

你可能感兴趣的:(scala小例子)