猿问

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

我刚刚开始学习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");    



绝地无双
浏览 100回答 1
1回答

蝴蝶不菲

您不需要在这里执行要执行的检查,只需循环就足够了。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");    }}
随时随地看视频慕课网APP

相关分类

Java
我要回答