Try-Catch循环回while循环Java

时间:2017-12-05 21:55:17

标签: java loops try-catch

我有一个项目来创建一个程序来选择一个数字(1-10),然后让用户猜测。如果它们太高,它就会告诉它们,如果它们猜得太低也会一样。现在要对它进行错误验证,我必须添加try和catch块。如果您查看代码,您将看到我有,但是当触发代码处理时,catch会完成。如何让它循环回来?

    import java.util.*;
    import java.lang.*;

    class GuessDR {

        public static void main(String[] args){



            System.out.println("Welcome to my number guessing game!");

            int maxnum = 10;
            Scanner input = new Scanner(System.in);
            Random rand = new Random();
            int number = rand.nextInt(maxnum);
            int tries = 0;         
            int guess;
            boolean win = false;

             try{

               while (win == false){ 



                   System.out.println("Guess a number between 1 and "+                     
                   maxnum +": ");
                   guess = input.nextInt();
                   tries++;    
                   if (guess == number){
                       win = true; 
                   }

                   else if(guess < number){
                       System.out.println("Number is to low, tray again");

                   }

                   else if(guess > number){
                       System.out.println("Number is to high, try again");

                   }

               }
            }

            catch (InputMismatchException e) {

                System.out.println("Enter numerical guess");            

            }
     System.out.println("You win!");
     System.out.println("It took you "+ tries + " tries.");

    }
 }

2 个答案:

答案 0 :(得分:1)

只需将try-catch移动到while循环中,并在catch块中提供正确的input.nextLine()方法,以清除缓冲区中的无效输入:

   import java.util.*;
   import java.lang.*;

    class GuessDR {

        public static void main(String[] args) {

            System.out.println("Welcome to my number guessing game!");

            int maxnum = 10;

            Random rand = new Random();
            int number = rand.nextInt(maxnum);
            int tries = 0;
            int guess;
            boolean win = false;
            Scanner input = new Scanner(System.in);

            while (win == false) {
                try {

                    System.out.println("Guess a number between 1 and " + maxnum + ": ");
                    guess = input.nextInt();
                    tries++;
                    if (guess == number) {
                        win = true;
                    }

                    else if (guess < number) {
                        System.out.println("Number is to low, tray again");

                    }

                    else if (guess > number) {
                        System.out.println("Number is to high, try again");

                    }

                } catch (InputMismatchException e) {
                    input.nextLine();
                    System.out.println("Enter numerical guess");

                }
            }

            System.out.println("You win!");
            System.out.println("It took you " + tries + " tries.");

        }
    }

答案 1 :(得分:0)

将try-catch移动到while循环中并处理扫描程序以预先设置无限循环。看看:https://stackoverflow.com/a/3572233/5964489

while (win == false)
        {

            System.out.println("Guess a number between 1 and "
                    + maxnum + ": ");
            try
            {
                guess = input.nextInt();
                tries++;
                if (guess == number)
                {
                    win = true;
                } else if (guess < number)
                {
                    System.out.println("Number is to low, tray again");

                } else if (guess > number)
                {
                    System.out.println("Number is to high, try again");

                }

            } catch (InputMismatchException e)
            {
                System.out.println("Enter numerical guess");
                input.next();
            }
        }
相关问题