猿问

异常后如何继续循环do while

程序读取异常后停止。


需要一点帮助才能使其继续到循环的开头。


我尝试了 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");            

        }    

    }    

}


MMTTMM
浏览 187回答 3
3回答

心有法竹

将 try/catch 移动到循环内并将其放置在引发异常的特定代码周围。do{&nbsp; &nbsp; System.out.println("Guess a number from 1 to 50");&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; guess = keyboard.nextInt();&nbsp; &nbsp; } catch (InputMismatchException e){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid Input");&nbsp; &nbsp; &nbsp; &nbsp; keyboard.readLine();&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; }&nbsp; &nbsp; count ++;&nbsp; &nbsp; // rest of codewhile(guess != secretNumber);&nbsp;我不确定count当你遇到异常时你想如何处理,如果你想计算每一次尝试,即使是不正确的尝试,然后count++在你从扫描仪读取之前移动到。

至尊宝的传说

尝试这个:public class LabExer5A {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int max = 50;&nbsp; &nbsp; &nbsp; &nbsp; int min = 1;&nbsp; &nbsp; &nbsp; &nbsp; int secretNumber;&nbsp; &nbsp; &nbsp; &nbsp; secretNumber = (int)(Math.random() * 49 + 1);&nbsp; &nbsp; &nbsp; &nbsp; Scanner keyboard = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; // you should initiate the value. If there is no exception, it would be replaced by the value read from console.&nbsp; &nbsp; &nbsp; &nbsp; int guess = Integer.MAX_VALUE;&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Guess a number from 1 to 50");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guess = keyboard.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch(InputMismatchException e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid Input");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // you should really read the input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyboard.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count ++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count ++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(guess == secretNumber){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(count> 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You got it in " + count + " attempt(s)");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You got it in " + count + " attempt");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(guess > max){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Out of Range");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(guess < min){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Out of Range");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(guess > secretNumber){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Too High. Try Again");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(guess < secretNumber){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Too Low. Try Again");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(guess != secretNumber);&nbsp; &nbsp; &nbsp; &nbsp; }}

慕侠2389804

package labexer5a; import java.util.*;public class LabExer5A {public static void main(String[] args) {&nbsp; int max = 50;&nbsp; int min = 1;&nbsp; int secretNumber;&nbsp; secretNumber = (int)(Math.random() * (max-1) + min);&nbsp; Scanner keyboard = new Scanner(System.in);&nbsp; int guess;&nbsp; int count = 0;&nbsp; do {&nbsp; &nbsp; System.out.println("Guess a number from "+min+" to "+max);&nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; guess = keyboard.nextInt();&nbsp; &nbsp; }&nbsp; &nbsp; catch(InputMismatchException e){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid Input");&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; } finally { // don't forget finally clause to increase count&nbsp; &nbsp; &nbsp; &nbsp; count ++;&nbsp; &nbsp; }&nbsp; &nbsp; if(guess == secretNumber){&nbsp; &nbsp; &nbsp; &nbsp; if(count> 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You got it in " + count + " attempt(s)");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You got it in " + count + " attempt");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else if(guess > max){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Out of Range");&nbsp; &nbsp; }&nbsp; &nbsp; else if(guess < min){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Out of Range");&nbsp; &nbsp; }&nbsp; &nbsp; else if(guess > secretNumber){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Too High. Try Again");&nbsp; &nbsp; }&nbsp; &nbsp; else if(guess < secretNumber){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Too Low. Try Again");&nbsp; &nbsp; }&nbsp; }&nbsp; while(guess != secretNumber);&nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答