【问题标题】:How to change random number after the user has guessed it right and displays message when user has input a wrong data type用户猜对后如何更改随机数并在用户输入错误数据类型时显示消息
【发布时间】:2016-03-22 02:57:56
【问题描述】:

我正在制作一个随机数猜测应用程序,但该应用程序只能运行一次,您必须重新启动该应用程序才能再次播放,我如何通过更改随机数让用户猜到正确结果后再次播放再次编号。 而且,我希望应用程序检测用户是否在文本字段中输入了错误的数字或字符串,我该怎么做? 任何帮助将不胜感激,谢谢!

    int randomNumber;

public void output(View view)
{
    EditText guess = (EditText) findViewById(R.id.editText);
    String guessedNumberString = guess.getText().toString();

    int guessedNumberInt = Integer.parseInt(guessedNumberString);

    String message = "";
    try {
        if (guessedNumberInt > randomNumber) {
            message = "Too high";
        } else if (guessedNumberInt < randomNumber) {
            message = "Too low";
        } else {
            message = "You are right! Try it again!";
        }
    }
    catch (InputMismatchException e)
    {
        message = "Enter a valid number";
    }
    Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Random randomGenerator = new Random();
    randomNumber = randomGenerator.nextInt(21);
}

【问题讨论】:

标签: java android exception


【解决方案1】:

逻辑如下:

 if(input == randNum){


ReLaunchGame();

}

现在,对于方法ReLaunchGame(),您必须重新启动活动,或重置所有文本字段。

只需将他们的输入与您选择的随机数进行比较。现在,看看他们是否输入了错误的输入。您必须首先了解异常处理,我认为您会这样做。

(http://www.mathcs.emory.edu/)

Scanner sc=new Scanner(System.in);
try
{
  System.out.println("Please input an integer");
  //nextInt will throw InputMismatchException
  //if the next token does not match the Integer
  //regular expression, or is out of range
  int usrInput=sc.nextInt();
}
catch(InputMismatchException exception)
{
  //Print "This is not an integer"
  //when user put other than integer
  System.out.println("This is not an integer");
}

就是这样。让我知道它是否有效! :-)

【讨论】:

    【解决方案2】:

    我知道有两种方法: 1. 创建一个 startGame 方法来设置游戏,即生成随机数,并在用户猜到后在您的输出方法中调用它 2.用它来刷新整个活动finish(); startActivity(getIntent());

    对于第二部分,在转换为整数之前尝试一些 if else

    【讨论】:

      【解决方案3】:

      要检查用户是否输入了数字,请将parseInt() 行放在try 块内。 您必须在此处捕获的异常是NumberFormatException。 所以它应该是这样的:

      try {
          int guessedNumberInt = Integer.parseInt(guessedNumberString);
          ...
      }
      catch (NumberFormatException e) {
          message = "Enter a valid number";
      }
      

      我对Android不熟悉,所以不知道那个output()函数是什么时候调用的。但是,您可以尝试在 try 块之前创建一个标志,以指示用户是否正确猜到了数字。然后,如果标志为true,则在函数末尾创建另一个随机数。您还需要将randomGenerator 设为类变量。

      Random randomGenerator = new Random();
      public void output(View view)
      {
          ...
          boolean correct = false;
          try {
              ...
              } else {
                  message = "You are right! Try it again!";
                  correct = true;
              }
          }
          ...
          Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
          if (correct) {
              randomNumber = randomGenerator.nextInt(21);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-11-28
        • 2021-06-01
        • 2022-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多