简体   繁体   中英

Switch and loop while in Java

I need your help concerning my switch .

Here is an example:

1) I enter the choice 1

*****MENU*****
1) Start Play : 
2) Exit : 
Enter your choice : 1

2) I enter the number 4

You have 5 attemps.
Enter your number : 4
Bravo ! 

3) I Won !

I again want to play...

I enter the number 1 in the option 1.

Bravo ! 
*****MENU*****
1) Start Play : 
2) Exit : 
Enter your choice : 1

Here, I have a problem... I cannot enter in the option 1 and then play ???

I always have this:

Enter your choice : 1
Option 1 : 
*****MENU*****
1) Start Play : 
2) Exit : 
Enter your choice : 

I think my problem is in my case 1 ?

int choice_user = 0;
int number_to_search = 4;
int number_user = 0;
boolean number_found = false;
int attemps = 5;

do {
    System.out.println("*****MENU*****");
    System.out.println("1) Start Play : ");
    System.out.println("2) Exit : ");
    System.out.print("Enter your choice : ");
    choice_user = sc.nextInt();

    switch (choice_user) {
        case 1:
            System.out.println("Option 1 : ");

            while (number_to_search > 0 && !number_found) {
                System.out.println("You have " + attemps + " attemps.");
                System.out.print("Enter your number : ");
                number_user = sc.nextInt();

                if (number_user > number_to_search) {
                    System.out.println("Smallest ! ");
                } else if (number_user < number_to_search) {
                    System.out.println("Biggest ! ");
                } else {
                    number_found = true;
                    System.out.println("Bravo ! ");
                }
                attemps--;
            }
            break;

        default:
            System.out.println("Exit...");
    }
} while (choice_user != 2);

You have a couple problems with the logic of your code.

The main issue, and why it keeps looping again without changing after the first iteration is because you do not reset the value of number_found back to false for the next game.

Additionally, you need to reset attemps back to 5 , and also check while (attemps > 0) , instead of number_to_search , which does not change.

The changed code will look like this below:

public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        int choice_user = 0;
        int number_to_search = 4;
        int number_user = 0;

        do{
           System.out.println("*****MENU*****");
           System.out.println("1) Start Play : ");
           System.out.println("2) Exit : ");
           System.out.print("Enter your choice : ");
           choice_user = sc.nextInt();

           int attemps = 5;
           boolean number_found = false;

           switch(choice_user){
               case 1 :
                  System.out.println("Option 1 : ");

                   while(attemps > 0 && !number_found){
                        System.out.println("You have " + attemps + " attemps.");
                        System.out.print("Enter your number : ");
                        number_user = sc.nextInt();

                        if(number_user > number_to_search){
                            System.out.println("Smallest ! ");
                        }
                        else if(number_user < number_to_search){
                            System.out.println("Biggest ! ");
                        }
                        else{
                            number_found = true;
                            System.out.println("Bravo ! ");
                        }
                        attemps--;
                   }
                 break;
                 default:
                       System.out.println("Exit...");
           }
        }while(choice_user != 2);
    }

Notice how I moved int attemps = 5; and boolean number_found = false; into the do-while loop. I tested the code, and it seemed to be working correctly for me now.

You forgot to set number_found on false again after case 1 is finished, so when entering again you are skipping your while loop since number_found is still on true . You simply need to do a number_found = false at the beginning of case 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM