Я хотел бы поставить необязательный "неправильный номер. Попробуйте еще раз", когда - PullRequest
0 голосов
/ 28 марта 2019

Я только начал изучать java-коды, поэтому у меня может возникнуть простой вопрос, который не прост для меня.

Я хотел бы указать необязательный «неправильный номер. Попробуйте еще раз», когда вводится по-другомуномер, чем секретный номер.Можете ли вы, ребята, помочь мне с этим кодом?

// Мне нужно узнать, как поставить «попробуйте еще раз», когда число! = Чем угадать число.

    /* 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");    
        }

        do {
        System.out.println("Guess what is the number 11 to 20: ");
        if (sc.hasNextInt()) {
            guess = sc.nextInt ();
        }
        }while(secretNum2 != guess);{
        System.out.println("Congratulations");
        System.out.println();
        System.out.println("The End");
        }
        }
        }
````````

1 Ответ

0 голосов
/ 28 марта 2019

Вам не нужно 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");

    }
}
...