亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

我想放一個可選的“錯誤號碼。重試“,當

我想放一個可選的“錯誤號碼。重試“,當

絕地無雙 2022-09-07 17:15:45
我剛剛開始學習java代碼,這就是為什么我有一個簡單的問題對我來說并不簡單。我想放一個可選的“錯誤號碼。重試“,當輸入的編號與 secretNum 不同時。你們可以幫我寫這個代碼嗎?我需要學習如何在數字為!=而不是猜測數字時放置“重試”。    /* I have tried     * 1)Change the signal "==" or "!=".     * 2) do {        System.out.println("Guess what is the number 0 to 10: ");        if (sc.hasNextInt()) {            guess = sc.nextInt();        }    } while(secretNum != guess);{    System.out.println("Well done");    System.out.println();    System.out.println("Are you ready for the next step?");    System.out.println();    }     */import java.util.Scanner;public class GuessNumber {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.println("Enter name:");        if(sc.hasNextLine()) {            String userName = sc.nextLine();            System.out.println("Hello " + userName + ",");            System.out.println();        }        int secretNum = 5;        int secretNum2 = 15;        int guess = 0;        do {            System.out.println("Guess what is the number 0 to 10: ");            if (sc.hasNextInt()) {                guess = sc.nextInt();            }        } while(secretNum != guess);{        System.out.println("Well done\n");        System.out.println("Are you ready for the next step?\n");        }         // I need learn how put "try again" when the number is != than guess number.         /* I have tried         * 1)Change the signal "==" or "!=".         * 2) do {            System.out.println("Guess what is the number 0 to 10: ");            if (sc.hasNextInt()) {                guess = sc.nextInt();            }        } while(secretNum != guess);{        System.out.println("Well done");        System.out.println();        System.out.println("Are you ready for the next step?");        System.out.println();        }         */        System.out.println("Enter Yes or No"); while(!sc.next().equals("yes")&& !sc.next().equals("no"));{    System.out.print("Yes");    
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

您不需要在這里執行要執行的檢查,只需循環就足夠了。do{} while()while(){}


請嘗試以下代碼:


import java.util.Scanner;


public class GuessNumber {


    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);

        System.out.println("Enter name:");

        if (sc.hasNextLine()) {

            String userName = sc.nextLine();

            System.out.println("Hello " + userName + ",");

            System.out.println();

        }


        int secretNum = 5;

        int secretNum2 = 15;

        int guess = 0;


        System.out.println("Guess what is the number 0 to 10: ");


        if (sc.hasNextInt()) {

            guess = sc.nextInt();

        }


        while (secretNum != guess) {

            System.out.println("Please try again\n");

            if (sc.hasNextInt()) {

                guess = sc.nextInt();

            }


        }

        System.out.println("Well done\n");

        System.out.println("Are you ready for the next step?\n");


        System.out.println("Enter Yes or No");

        while (!sc.next().equals("yes") && !sc.next().equals("no"))

        {

            System.out.print("Yes");

        }


        System.out.println("Guess what is the number 11 to 20: ");


        if (sc.hasNextInt()) {

            guess = sc.nextInt();

        }


        while (secretNum2 != guess) {

            System.out.println("Please try again\n");

            if (sc.hasNextInt()) {

                guess = sc.nextInt();

            }


        }


        System.out.println("Congratulations");

        System.out.println();

        System.out.println("The End");


    }

}


查看完整回答
反對 回復 2022-09-07
  • 1 回答
  • 0 關注
  • 91 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號