第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

異常后如何繼續(xù)循環(huán)do while

異常后如何繼續(xù)循環(huán)do while

MMTTMM 2022-07-14 17:29:38
程序讀取異常后停止。需要一點幫助才能使其繼續(xù)到循環(huán)的開頭。我嘗試了 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貢獻(xiàn)1866條經(jīng)驗 獲得超5個贊

將 try/catch 移動到循環(huán)內(nèi)并將其放置在引發(fā)異常的特定代碼周圍。


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當(dāng)你遇到異常時你想如何處理,如果你想計算每一次嘗試,即使是不正確的嘗試,然后count++在你從掃描儀讀取之前移動到。


查看完整回答
反對 回復(fù) 2022-07-14
?
至尊寶的傳說

TA貢獻(xiàn)1789條經(jīng)驗 獲得超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);

        }

}


查看完整回答
反對 回復(fù) 2022-07-14
?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗 獲得超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);


  }

}


查看完整回答
反對 回復(fù) 2022-07-14
  • 3 回答
  • 0 關(guān)注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號