用户输入验证 - hasnext()while

时间:2017-12-15 00:02:11

标签: java java.util.scanner do-while boolean-logic next

我创建了一款游戏,可以在1&之间打印一个随机卷号。 10.然后,系统询问用户是否想要为第二个卷号猜测更高[1]或更低[2]。基于他们的猜测,系统将打印“Draw!”,“你猜错了”。或者“你猜对了。”然后,程序会提示用户是否要使用while循环重新启动程序(是[y])。

但是,现在我正在尝试验证整数'Guess'用户输入,以便只能分别输入[1] / [2]。我使用while循环使用hasNext()方法设置这些条件有困难。在正确或不正确的用户输入插入while循环后,我的程序现在不会继续。

请参阅“//无效的用户输入”块

代码:

public static void main(String args[]) {
    String replay = "y";
    Scanner input = new Scanner (System.in);
    while(replay.equals("y"))
    {

        //roll one
        int rNum1 = (int)(Math.random() * 11); //declare random roll one,  gives result between 1 and 10

        //print roll one
        System.out.println("You roll the 10-sided die."); 
        System.out.println("It lands on " + rNum1 + ".");
        System.out.println(); //space

        //User guess
        System.out.println("Will a second die roll higher or lower?");
        System.out.println("To guess higher enter [1], or to guess lower, enter [2].");

        //roll two
        int rNum2 = (int)(Math.random() * 11); //declare random roll two
        int guess = input.nextInt(); //user's guess / HiLo

        //invalid user entry
        while(!input.hasNextInt() || (guess != 1 && guess != 2)) 
        {
            System.out.println("Invalid entry"); // print incorrect guess entry input
            System.out.println("To guess higher enter [1], or to guess lower, enter [2].");
            input.next(); //reset scanner "input"
        }

        //print roll two
        System.out.println(); //space
        System.out.println("You roll the die again."); 
        System.out.println("It lands on " + rNum2 + ".");
        System.out.println(); //space


        if (guess == 1) //guessed higher
        {
            if (rNum1 == rNum2)
            {
                System.out.println("Draw!");
            }
            else if (rNum1 > rNum2)
            {
                System.out.println("You guessed wrongly.");
            }
            else
            {
                System.out.println("You guessed correctly!");
            }
        }

        if (guess == 2) //guessed lower
        {
            if (rNum1 == rNum2)
            {
                System.out.println("Draw!");
            }
            else if (rNum1 > rNum2)
            {
                System.out.println("You guessed correctly!");
            }
            else
            {
                System.out.println("You guessed wrongly.");
            }
        }
        System.out.println("Would you like to play again? Enter [y] for yes.");
        replay = input.nextLine();
    }
}

1 个答案:

答案 0 :(得分:0)

只需检查猜测的值,如果它不满足要求,请再次询问

    int guess = input.nextInt(); //user's guess / HiLo

    //invalid user entry
    while(guess != 1 && guess != 2) 
    {
        System.out.println("Invalid entry"); // print incorrect guess entry input
        System.out.println("To guess higher enter [1], or to guess lower, enter [2].");
        guess = input.nextInt(); //reset scanner "input"
    }

这样的东西适用于用户输入的任何内容,我们抓住整行并查看它是否与数字匹配,如果是,则我们将输入解析为整数并报告该值是否为1或2并继续询问输入,直到用户输入所需的1或2

      int guess = 0;
      while(guess != 1 && guess != 2)
      {
          String inputFromUser = input.nextLine();
          if(inputFromUser.matches("\\d+"))
          {
              guess = Integer.parseInt(inputFromUser);
              if(guess != 1 && guess != 2)
                  System.out.println("Value must be either a 1 or 2");
          }
          else
              System.out.println("Invalid entry");
      }