简体   繁体   中英

Why is my switch statement not working and stuck in an infinite loop?

I'm trying to get it to output the users fortune, but it will only output the number the user entered infinitely.

What am I doing wrong? How can I fix the infinite loop?

public static void main(String[] args) {

    Scanner broncos = new Scanner ( System.in );

    int x, y, a;

    System.out.println("Would you like me to tell your fortune? Type 1 for yes and any other number for no: ");
    x = broncos.nextInt();


    while (x==1)
    {   
        System.out.println("Enter a number from 1-8: ");
        y = broncos.nextInt();

        while ( y > 0 && y < 9)

        System.out.println( y );
        System.out.print ( "That's not a valid number. Try again. ");
        y = broncos.nextInt();
    }


    System.out.println ("Enter a number from 1-8: ");
    a = broncos.nextInt();


    switch (a) 
    {
        case 8 : 
            System.out.println ("You will win the lottery" );
            break; 
        case 7 : 
            System.out.println ("You are going to do great things");
            break; 
        case 6 : 
            System.out.println ("Don't get out of the house today");
            break; 
        case 5 : 
            System.out.println ("You'll get a raise at work");
            break;  

        case 4 : 
            System.out.println ("Lucky day today");
            break; 

        case 3 : 
            System.out.println ("Not so lucky day today");
            break; 

        case 2 : 
            System.out.println ("Your favorite sports team will win" );
            break; 

        case 1 :
            System.out.println ("Your car is going to break down" );
            break;

        default: 
            System.out.println ( "You didn't enter 1-8!" );
    }

    broncos.close();
}

It's not your switch statement that's causing problems but rather the while loop prior. You test x as in while (x==1) , but where do you change x within the while loop? If you never change the state of x , the test will never become false, and the loop will never exit.

As Tom astutely points out, you will want your while loop that validates your y input, to enclose all the code that is related to getting the y input.

while ( y > 0 && y < 9) {
    System.out.println("Enter a number from 1-8: ");
    y = broncos.nextInt();

    if (y > 0 && y < 9) {    
        System.out.println( y );
        System.out.print ( "That's not a valid number. Try again. ");
    }
}

Now use your correct y in your switch statement.

Also, once you have a correct y , there's no need to get an a :

// System.out.println ("Enter a number from 1-8: ");
// a = broncos.nextInt();

Again, just use y in the switch statement.

As for your x , there's no need to validate it (unless you need to check to see that it is infact a number) since any number entered should work.

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