异常后如何继续执行循环

时间:2019-03-02 08:47:36

标签: java

程序读取异常后,它将停止。

在如何使其继续循环的开始方面需要一点帮助。

我尝试了continue语句,但是它没有用,或者可能是我的错误。

package labexer5a;
import java.util.*;

public class LabExer5A {

    public static void main(String[] args) {
        int max = 50;
        int min = 1;

        int secretNumber;
        secretNumber = (int)(Math.random() * 49 + 1);

        Scanner keyboard = new Scanner(System.in);
        int guess;
        int count = 0;
        try{
            do{
                System.out.println("Guess a number from 1 to 50");
                guess = keyboard.nextInt();
                count ++;

                if(guess == secretNumber){
                    if(count> 1){
                    System.out.println("You got it in " + count + " attempt(s)");
                    }
                    else{
                        System.out.println("You got it in " + count + " attempt");
                    }
                }
                else if(guess > max){
                    System.out.println("Out of Range");
                }
                else if(guess < min){
                    System.out.println("Out of Range");
                }
                else if(guess > secretNumber){
                    System.out.println("Too High. Try Again");
                }
                else if(guess < secretNumber){
                    System.out.println("Too Low. Try Again");
                }                
            }
            while(guess != secretNumber);
        }

        catch(InputMismatchException e){           
            System.out.println("Invalid Input");            
        }    
    }    
}

3 个答案:

答案 0 :(得分:8)

在循环中移动try / catch并将其放置在引发异常的特定代码周围。

do{
    System.out.println("Guess a number from 1 to 50");
    try {
        guess = keyboard.nextInt();
    } catch (InputMismatchException e){
        System.out.println("Invalid Input");
        keyboard.readLine();
        continue;
    }
    count ++;
    // rest of code
while(guess != secretNumber); 

我不确定在遇到异常时如何处理count,如果您想计算每一次尝试,即使是不正确的尝试,都应将count++移至从扫描仪读取之前。

答案 1 :(得分:0)

package labexer5a; import java.util.*;

public class LabExer5A {

public static void main(String[] args) {

  int max = 50;
  int min = 1;

  int secretNumber;
  secretNumber = (int)(Math.random() * (max-1) + min);

  Scanner keyboard = new Scanner(System.in);
  int guess;
  int count = 0;
  do {
    System.out.println("Guess a number from "+min+" to "+max);

    try{
        guess = keyboard.nextInt();
    }
    catch(InputMismatchException e){

        System.out.println("Invalid Input");
        continue;

    } finally { // don't forget finally clause to increase count
        count ++;
    }

    if(guess == secretNumber){
        if(count> 1){
            System.out.println("You got it in " + count + " attempt(s)");
        }
        else{
                System.out.println("You got it in " + count + " attempt");
        }
    }
    else if(guess > max){
            System.out.println("Out of Range");
    }
    else if(guess < min){
            System.out.println("Out of Range");
    }
    else if(guess > secretNumber){
            System.out.println("Too High. Try Again");
    }
    else if(guess < secretNumber){
            System.out.println("Too Low. Try Again");
    }

  }
  while(guess != secretNumber);

  }
}

答案 2 :(得分:0)

尝试一下:

public class LabExer5A {
    public static void main(String[] args) {
        int max = 50;
        int min = 1;

        int secretNumber;
        secretNumber = (int)(Math.random() * 49 + 1);

        Scanner keyboard = new Scanner(System.in);
        // you should initiate the value. If there is no exception, it would be replaced by the value read from console.
        int guess = Integer.MAX_VALUE;
        int count = 0;

            do{
                System.out.println("Guess a number from 1 to 50");
                try {
                    guess = keyboard.nextInt();
                } catch(InputMismatchException e){
                    System.out.println("Invalid Input");
                    // you should really read the input
                    keyboard.next();
                    count ++;
                    continue;
                }

                count ++;

                if(guess == secretNumber){
                    if(count> 1){
                        System.out.println("You got it in " + count + " attempt(s)");
                    }
                    else{
                        System.out.println("You got it in " + count + " attempt");
                    }
                }
                else if(guess > max){
                    System.out.println("Out of Range");
                }
                else if(guess < min){
                    System.out.println("Out of Range");
                }
                else if(guess > secretNumber){
                    System.out.println("Too High. Try Again");
                }
                else if(guess < secretNumber){
                    System.out.println("Too Low. Try Again");
                }
            }
            while(guess != secretNumber);
        }
}