为了账号安全,请及时绑定邮箱和手机立即绑定

异常后如何继续循环do while

异常后如何继续循环do while

MMTTMM 2022-07-14 17:29:38
程序读取异常后停止。需要一点帮助才能使其继续到循环的开头。我尝试了 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 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

将 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++在你从扫描仪读取之前移动到。


查看完整回答
反对 回复 2022-07-14
?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

尝试这个:


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);

        }

}


查看完整回答
反对 回复 2022-07-14
?
慕侠2389804

TA贡献1719条经验 获得超6个赞

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);


  }

}


查看完整回答
反对 回复 2022-07-14
  • 3 回答
  • 0 关注
  • 186 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号