简体   繁体   English

如何在java中的while循环中设置switch语句

[英]how to set a switch statement in while loop in java

I want to do a switch in while loop where at the break of every switch statement the while loop stops and ask for an input like F, R, C, Q. The statement below works but the statement does not break. 我想在while循环中进行切换,在每个switch语句中断while循环停止并请求输入,如F,R,C,Q。下面的语句有效,但语句不会中断。 Please help 请帮忙

public static void main(String[] args) throws IOException {

    // start both with 1 point
    int goodTotal = 50;
    int monTotal = 50;

    // input switch statement

    while (goodTotal > 0 && monTotal > 0) {

        System.out.print("Type a letter: ");
        System.out.println("\n");
        System.out.print("F: Go out and Fight ");
        System.out.println("\n");
        System.out.print("R: Rest ");
        System.out.println("\n");
        System.out.print("C: Check Stats ");
        System.out.println("\n");
        System.out.print("Q: Quit ");
        int input = System.in.read();

        System.out.println("You typed: " + (char) input);

        switch (input) {
        case 'f':
            System.out.println("Continue the game");
            break;
        case 'r':
            System.out.println("Players should rest");
            break;
        case 'c':
            System.out.println("Checking the status of the game");
            System.out.print("Goodman has " + goodTotal + " points and Monster has " + monTotal + " points");
            System.out.println("\n");
            break;
        case 'q':
            System.out.println("Game over");
            System.exit(input);
            break;
        default:
            System.out.println("Invalid selection");
            break;
        }

        // Set value of minimum and maximum damage
        int minDmg = 2;
        int maxDmg = 15;

        // Get random number;
        int damage = minDmg + Double.valueOf(Math.random() * (maxDmg - minDmg)).intValue();
        int damage2 = minDmg + Double.valueOf(Math.random() * (maxDmg - minDmg)).intValue();

        // remove value of damage from started value to get total value remaining
        goodTotal = goodTotal - damage;
        monTotal = monTotal - damage2;

        // print message if still in the game
        if (goodTotal > 0) {
            System.out.println("Goodman has " + goodTotal + " points left. Not bad, Man! ");
        }

        // if Goodman survives round 2 print a message of encouragement
        if (goodTotal > 0 && count > 1 && count <= 2) {
            System.out.print("This is encouraging. Goodman has lasted past roundhh " + count + ". ");

            // print new message if Goodman passes round 3
        } else if (goodTotal > 0 && count == 3) {
            System.out.print("Goodman is as strong as Samson. He has lasted round " + count
                    + " and still looks strong.");
            System.out.print(" 10 hit points has been added to your total");
        }

        if (monTotal > 0) {
            System.out.println("Wait, Monster has a total of " + monTotal + " points and is still in the game");
        }

        // exit if have less than 0 point, and print game over. Congratulate the winner
        if (goodTotal < 0) {
            System.out.println("Goodman you are out of the game");
            System.out.println("The monster will take over the village. This is sad");
            System.out.println("Game Over!");
        } else if (monTotal < 0) {
            System.out.println("Goodman has been victorious");
            System.out.println("The monster is dead. The people live!!!!");
            System.out.println("Game Over!");
        }
        System.out.println("This is the end of round " + count + " ");
        System.out.println("\n");
        count = count + 1;

    }

}

Use a label on the loop: 在循环上使用标签:

loop: while (goodTotal > 0 && monTotal > 0) {
    // ...
    switch (input) {
        case 'f':
            // ...
            break loop;
        case 'r':
            // ...
            break loop;
        // ...
    }
    // ...
}

You should use labelled break s. 你应该使用带标签的break

Although it probably is better to rewrite the code in a way that doesn't need them, as they're not very easy to read. 尽管以不需要它们的方式重写代码可能更好,因为它们不是很容易阅读。

input is of type int , but the case labels are character literals ( ie, f,r,c,q ). input的类型为int ,但case标签是字符文字 (即f,r,c,q )。 Why don't you just make the input also a char type ? 你为什么不把input也变成char类?

char input = System.in.read();

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

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