猜测号的InputMismatchException

时间:2017-01-15 23:31:12

标签: java netbeans-7

我正在做一个猜谜游戏,我可以输入1-100但是我只是在接受数字时遇到麻烦,如果我在第一次运行程序时键入一个字母,它会给我错误并立即执行程序{{ 3}}但是如果在我启动程序并输入下一个字母之后的数字,它会给我一个错误的消息,它应该只显示消息“无效输入”。image here任何建议都要感谢。

  package m1;
  import java.util.InputMismatchException;
   import java.util.Scanner;


  public class M1{


public static void main(String[] args) {

    Scanner Scanner = new Scanner(System.in);

    int between = 100;
    int secretNumber = (int)(Math.random()*between);
    int inputNum = 0;
    int guesses = 0;

    System.out.println("Please enter your guess: ");
    inputNum = Scanner.nextInt();
    guesses++;


    while (inputNum != secretNumber) {  

        try {

        // number too high or too low
        if (inputNum > 100 || inputNum < 1) {
            System.out.println("Out of Range!");
            System.out.println("Enter a guess between 1 and " + between + ".");
            inputNum = Scanner.nextInt();
        }

        // less than secretNumber
        if (inputNum < secretNumber) {
            System.out.println("Too Low...Try Again!");
            inputNum = Scanner.nextInt();
            guesses++;
        }

        // greater than secretNumber
      if (inputNum > secretNumber) {
        System.out.println("Too High...Try Again!");
        inputNum = Scanner.nextInt();
        guesses++;
    }
    }
    catch(InputMismatchException e){
        System.out.println("Invalid Input");
        Scanner.next();

    }
    }


    System.out.println("\nWell done! The secret number was " + secretNumber + "." + "\nYou took " + guesses +  " guesses.");
    }

}

1 个答案:

答案 0 :(得分:0)

  • 在大多数情况下,通常使用camelCase在java中命名变量名称。
  • 如果scanner.next()scanner.hasNextInt(),您实际上不需要捕获任何例外,因为您只需false import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int between = 100; int secretNumber = (int)(Math.random() * between); int guesses = 0; System.out.println("Lec's Guessing Game"); System.out.println("===================="); System.out.print("Please enter a guess between 1 and " + between + "(inclusive):"); while(scanner.hasNext()){ if(scanner.hasNextInt()){ int inputNum = scanner.nextInt(); guesses++; if(inputNum == secretNumber) { System.out.println("\nWell done! The secret number was " + secretNumber + "." + "\nYou took " + guesses + " guesses."); } // number too high or too low if (inputNum > between || inputNum < 1) { System.out.println("Out of Range!"); System.out.print("Enter a guess between 1 and " + between + "(inclusive):"); } // less than secretNumber if (inputNum < secretNumber) { System.out.println("Too Low...Try Again!"); System.out.print("Enter a guess between 1 and " + between + "(inclusive):"); } // greater than secretNumber if (inputNum > secretNumber) { System.out.println("Too High...Try Again!"); System.out.print("Enter a guess between 1 and " + between + "(inclusive):"); } } else{ System.out.print("ERROR: Invalid Input"); System.out.print("Please enter a number: "); scanner.next(); } } } } 。提示用户此次专门输入一个数字。

尝试以下代码here!

ListBoxItem