mercredi 27 mars 2019

I would like put a optional "wrong number. Try again" when is

I have just started learn java codes, that is why might I have a simple question that is not simple for me.

I would like put a optional "wrong number. Try again" when is entered different number than secretNum. May you guys help me out on this code?

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

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

Aucun commentaire:

Enregistrer un commentaire