简体   繁体   English

为什么我的switch语句不起作用并陷入无限循环?

[英]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. 引起问题的不是您的switch语句,而是while循环优先。 You test x as in while (x==1) , but where do you change x within the while loop? 您可以像while (x==1)一样测试x ,但是在while循环中在哪里更改x If you never change the state of x , the test will never become false, and the loop will never exit. 如果您从不更改x的状态,则测试将永远不会变为假,并且循环将永远不会退出。

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输入,以封装与获取y输入有关的所有代码。

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. 现在,在switch语句中使用正确的y

Also, once you have a correct y , there's no need to get an a : 另外,一旦您有一个正确的y ,就无需获取a

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

Again, just use y in the switch statement. 同样,只需在switch语句中使用y

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. 至于x ,则无需验证它(除非您需要检查它是否是一个数字),因为输入的任何数字都可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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