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

我想放一个可选的“错误号码。重试“,当

我想放一个可选的“错误号码。重试“,当

绝地无双 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 关注
  • 99 浏览

添加回答

举报

0/150
提交
取消
微信客服

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

帮助反馈 APP下载

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

公众号

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