Trying to use Try catch in a while loop until the user answers correctly without entering invalid data(Java)

匆匆过客 提交于 2019-12-25 07:58:43

问题


I am trying to create a program which asks the user to guess a number which is randomly generated and the loop exits when the input is correct. I am also trying to stop user from entering an invalid data and want the loop to repeat until user enters a valid data. The problem is when is type in an alphabet as an input, the program repeats. thank you for helping in advance. I am using eclipse kepler

Output:
Try guessing the number: k You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number:

while(true){
         try{
            System.out.println("Try guessing the number: ");
            guess=input.nextInt();
            if(guess==sum){
                System.out.println("You have guessed it correctly");
                break;
            }
        }catch(InputMismatchException e){
            System.out.println("You have entered invalid data. Please try again");
        }
       }

回答1:


This worked for me... It fixes the while(true) issue and uses a simpler way to check for incorrect data that is easier to debug! Enjoy and good luck with your game!

        String guessString="";
        int guess = 0;
        Scanner input = new Scanner(System.in);
        int sum = 12;
        //just used 12 as a placeholder, you'll have to connect your 
        //random number generator, as well as changing the default of 
        //guess=0 if 0 is in your range for the random number

        while(sum!=guess){

            System.out.println("Try guessing the number: ");
            guessString=input.next();
            try {
                guess = Integer.parseInt(guessString);
            } catch(Exception e) {
                System.out.println("Invalid Data.");
                guess=0;
            }

        }
        System.out.println("You have guessed it correctly");



回答2:


You don't have to use try/cathc structure. A while loop would be enough

while(true){
 System.out.println("Try guessing the number: ");
            guess=input.nextInt();

if(guess==sum){
                System.out.println("You have guessed it correctly");
                break;
            }
if(!(guess instanceof Int)){
System.out.println("You have entered invalid data. Please try again");

}


}


来源:https://stackoverflow.com/questions/27208796/trying-to-use-try-catch-in-a-while-loop-until-the-user-answers-correctly-without

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!