Java 使用try/catch时如何避免无限循环?

Java 使用try/catch时如何避免无限循环?,java,try-catch,Java,Try Catch,我有这个密码 public void askUserForStrategy(){ try{ System.out.println("What strategy do you want to use?\n"); System.out.println("1 = Math.random\t 2 = System time\t " + "3 = Sum of Math.random and System time");

我有这个密码

public void askUserForStrategy(){

    try{

        System.out.println("What strategy do you want to use?\n");
        System.out.println("1 = Math.random\t     2 = System time\t "
        + "3 = Sum of Math.random and System time");

        int strategy = sc.nextInt();

        selectStrategy(strategy);

    }

    catch(InputMismatchException Exception){

        System.out.println("That is not an integer. Try again.\n");
        askUserForStrategy();

    }

}
我想让它做的基本上是要求用户键入一个整数,如果用户键入一个字符串,例如,捕获该异常并再次启动该方法(要求用户键入一个整数值)。但当用户键入字符串时,该方法会循环

nextInt()
抛出异常时,
Scanner
对象在下次调用时尝试使用相同的字符串

尝试在
Try
中分配新的
扫描仪
对象。或者尝试调用
catch
中的
nextLine()
,以便丢弃非法行

请注意,这种方法不好,因为在太多非法输入(非常多,但理想情况是无限次尝试)后,将发生堆栈溢出

我建议您在
try
正文末尾使用
do-while
return

nextInt()
抛出异常时,
Scanner
对象在下次调用时尝试使用相同的字符串

尝试在
Try
中分配新的
扫描仪
对象。或者尝试调用
catch
中的
nextLine()
,以便丢弃非法行

请注意,这种方法不好,因为在太多非法输入(非常多,但理想情况是无限次尝试)后,将发生堆栈溢出

我建议您在
try
正文末尾使用
do-while
return

尝试以下方法:

public void askUserForStrategy() {

 for(int i=0; i<1; ++i) {
  try{

    System.out.println("What strategy do you want to use?\n");
    System.out.println("1 = Math.random\t     2 = System time\t "
    + "3 = Sum of Math.random and System time");

    int strategy = sc.nextInt();

    selectStrategy(strategy);
    break;  //break loop when there is no error
  }

  catch(InputMismatchException Exception){

    System.out.println("That is not an integer. Try again.\n");
    //askUserForStrategy();
    continue; //for clarity

  }
 }
}
public void askUserForStrategy(){
对于(int i=0;i请尝试以下方法:

public void askUserForStrategy() {

 for(int i=0; i<1; ++i) {
  try{

    System.out.println("What strategy do you want to use?\n");
    System.out.println("1 = Math.random\t     2 = System time\t "
    + "3 = Sum of Math.random and System time");

    int strategy = sc.nextInt();

    selectStrategy(strategy);
    break;  //break loop when there is no error
  }

  catch(InputMismatchException Exception){

    System.out.println("That is not an integer. Try again.\n");
    //askUserForStrategy();
    continue; //for clarity

  }
 }
}
public void askUserForStrategy(){

对于(inti=0;i也许你在寻找这样的东西

public void askUserForStrategy(){
Boolean loopFlag = true;
while(loopFlag) {
try{

    System.out.println("What strategy do you want to use?\n");
    System.out.println("1 = Math.random\t     2 = System time\t "
    + "3 = Sum of Math.random and System time");

    int strategy = sc.nextInt();
    Integer.parseInt(strategy);
    loopFlag  = false;
    selectStrategy(strategy);
}

catch(Exception e){

    //Parse has failed due to wrong input value, But loop will continue

}}}

也许你在找这样的东西

public void askUserForStrategy(){
Boolean loopFlag = true;
while(loopFlag) {
try{

    System.out.println("What strategy do you want to use?\n");
    System.out.println("1 = Math.random\t     2 = System time\t "
    + "3 = Sum of Math.random and System time");

    int strategy = sc.nextInt();
    Integer.parseInt(strategy);
    loopFlag  = false;
    selectStrategy(strategy);
}

catch(Exception e){

    //Parse has failed due to wrong input value, But loop will continue

}}}

这可能就是你要找的

public void askUserForStrategy() {
        while (true) {
            try {
                Scanner sc = new Scanner(System.in);
                System.out.println("What strategy do you want to use?\n");
                System.out.println("1 = Math.random\t     2 = System time\t " + "3 = Sum of Math.random and System time");

                int strategy = sc.nextInt();
                System.out.println("Selected strategy : " +strategy);
                break;
            } catch (Exception e) {
                System.out.println("That is not an integer. Try again.\n");
                continue;
            }
        }
        // selectStrategy(strategy);
    }
若用户选择字符串,那个么它将再次请求选项

如果用户选择Integer,则它将采用用户选择的策略选项并继续程序流程..(意味着在break的帮助下退出while循环,然后调用selectStrategy方法)


谢谢

这可能就是您要找的

public void askUserForStrategy() {
        while (true) {
            try {
                Scanner sc = new Scanner(System.in);
                System.out.println("What strategy do you want to use?\n");
                System.out.println("1 = Math.random\t     2 = System time\t " + "3 = Sum of Math.random and System time");

                int strategy = sc.nextInt();
                System.out.println("Selected strategy : " +strategy);
                break;
            } catch (Exception e) {
                System.out.println("That is not an integer. Try again.\n");
                continue;
            }
        }
        // selectStrategy(strategy);
    }
若用户选择字符串,那个么它将再次请求选项

如果用户选择Integer,则它将采用用户选择的策略选项并继续程序流程..(意味着在break的帮助下退出while循环,然后调用selectStrategy方法)


谢谢

不要对程序流使用
try-catch
。它应该只处理不可恢复的异常;不要重新尝试相同的过程。不要对程序流使用
try-catch
。它应该只处理不可恢复的异常;不要重新尝试相同的过程。谢谢!我刚刚在方法中移动了scanner对象,它工作正常非常好。谢谢!我刚刚在方法中移动了扫描仪对象,它工作得非常好。