how to set a switch statement in while loop in java

后端 未结 3 1235
日久生厌
日久生厌 2020-12-19 00:21

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

相关标签:
3条回答
  • 2020-12-19 00:40

    You should use labelled breaks.

    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.

    0 讨论(0)
  • 2020-12-19 00:46

    Use a label on the loop:

    loop: while (goodTotal > 0 && monTotal > 0) {
        // ...
        switch (input) {
            case 'f':
                // ...
                break loop;
            case 'r':
                // ...
                break loop;
            // ...
        }
        // ...
    }
    
    0 讨论(0)
  • 2020-12-19 01:01

    input is of type int, but the case labels are character literals ( i.e., f,r,c,q). Why don't you just make the input also a char type ?

    char input = System.in.read();
    
    0 讨论(0)
提交回复
热议问题