Scala骰子计划

时间:2017-11-03 20:50:29

标签: scala

我刚刚在大学开设了Scala,而我们目前只是在做基础知识,但我已经开始了,并希望尝试制定一些基本程序来取得进展。我目前正在尝试制作一个基本的骰子角色猜测程序。

我想添加一个功能来重放游戏,计数器是告诉用户有多少次尝试猜测。我知道我可以在计数器+ = 1的某个地方添加它,只是不确定程序中的位置。

目前编译什么都没做,直到我输入内容并且我不确定为什么,然后当我输入一些内容时,我不会从if语句中获得任何结果,它只是终止程序。

import scala.io.StdIn.readInt
import scala.util.Random

object GuessTheNumber {
 def main(args: Array[String]): Unit = {


var play = true
var counter = 0   //Allows counting input
var responseGuess = readInt()
val numRange = Random.nextInt(12)

println("Enter your guess of the result of 2, 6 sided dice.")

while(play) {

if (responseGuess == numRange)
  println ("You got it right!")

else if (numRange < responseGuess)
  ("Your guess was too large by" + (responseGuess - numRange) + " the corrrect answer was " + numRange)

else if (numRange > responseGuess)      
  ( "Your guess was too small by" + (numRange - responseGuess) + " the correct answer was " + numRange) 

else if (responseGuess < 1)
  ("Your guess is too small enter 2-12")

else if (responseGuess > 12)
  ("You entered an invalid number, 2 dice can only equate to a maximum of 12")

   } 
  }
 }

2 个答案:

答案 0 :(得分:0)

你有一些基本的错误,但是,这是一个非常好的第一次尝试。为清楚起见,我已将大部分意见纳入上下文。以下是一些需要微小修正的事情的快速摘要

  1. 您忘记在其他所有条款
  2. 中加入println
  3. 您没有重置play变量的值,导致您的程序陷入无限循环。 while循环继续执行而不会要求用户输入任何输入,从而产生无法打印的错觉
  4. 计数器需要在循环中的每次尝试后更新

    import scala.io.StdIn.readInt
    import scala.io.StdIn.readChar
    import scala.util.Random
    
    object GuessTheNumber {
      def main(args: Array[String]): Unit = {
        var play = true
        var counter = 1   //Allows counting input
    
        // Start a loop. As long as the user says he wants to continue, we keep entering the loop
        while (play) {
          // The game is slightly more clear in my humble opinion if you ask him/her this question after every attempt.
          // Otherwise you just have a blinking cursor on screen, without prompting user for input
          println("Enter your guess of the result of 2, 6 sided dice.")
          // Read his current response
          val responseGuess = readInt()
          // If you don't have `1 +`, then, 0 could be a potential result
          val numRange = 2 + Random.nextInt(11)
    
          if (responseGuess == numRange)
            println(s"You got it right! in $counter attempts")
          // You were missing println in each of the following cases. Hence the lack of response from your program, since,
          // it didn't know you wanted it to print that response
          else if (numRange < responseGuess)
            println("Your guess was too large by " + (responseGuess - numRange) + " the correct answer was " + numRange)
    
          else if (numRange > responseGuess)
            println( "Your guess was too small by " + (numRange - responseGuess) + " the correct answer was " + numRange)
    
          else if (responseGuess < 1)
            println("Your guess is too small enter 2-12")
    
          else if (responseGuess > 12)
            println("You entered an invalid number, 2 dice can only equate to a maximum of 12")
    
          println("Do you want to continue? (Y/N)")
          // Now you are reading a character instead of an integer. Basically a Y or an N
          play = readChar() match { // Not sure if you have gotten this far in your course. This syntax is referred to as pattern matching.
            // Its basically the same as using if / else
            case 'Y' | 'y' => true // If he enters Y or y, we assume he wants to continue.
            case _ => // For any other input we abort
              println(s"You tried $counter times") // When he does decide to quit, lets tell him how many times he tried
              false
          }
          // After every attempt increment counter within your loop
          counter += 1
        }
      }
    }
    
  5. 希望这有帮助。

答案 1 :(得分:0)

一些事情:

  • 在从输入
  • 获取之前,您应该要求猜测(println
  • 您的所有else if块都缺少println语句
  • 您没有使用counter
  • 您的numRange不正确,现在它将返回0到11(含)

我进行了这些更改,并将readInt()移到了while循环中,让人们有多种机会猜测,一旦他们猜到了答案,那么循环退出。

var play = true
val numRange = 2 + Random.nextInt(11)

while(play) {

  println("Enter your guess of the result of 2, 6 sided dice.")
  var responseGuess = readInt()

  if (responseGuess == numRange) {
    println ("You got it right!")
    play = false
  }

  else if (numRange < responseGuess)
    println("Your guess was too large")

  else if (numRange > responseGuess)
    println( "Your guess was too small")

  else if (responseGuess < 2)
    println("Your guess is too small enter 2-12")

  else if (responseGuess > 12)
    println("You entered an invalid number, 2 dice can only equate to a maximum of 12")

}

您可以构建此功能以获得所需的功能。