Java:带有 switch 语句的 do-while 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26520452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:42:10  来源:igfitidea点击:

Java: do-while loop with switch statement

javaswitch-statementdo-while

提问by ccaulfield

Language: Java

语言:Java

boolean x = false;
String letter = "NULL";

do{
    System.out.print("Enter grade (one character): ");
    letter = sc.next(); 
    switch (letter) {
       case "a": 
       x=true;
       break;
     case "A":
       x=true;
       break;      
     case "b":
       x=true;
       break;      
     case "B":
       x=true;
       break;      
     case "c":
       x=true;
       break;      
     case "C":
       x=true;
       break;      
     case "d":
       x=true;
       break;      
     case "D":
       x=true;
       break;
     case "f":
       x=true;
       break;      
     case "F":
       x=true;
       break;
     default:
       System.out.println("Invalid grade - must enter A,B,C,D,F (upper or lower case)");
       System.out.println(x);
       break;
     }
      System.out.println(x);
     }
      while(x=false);

--------------result output-----------------------------------result output--------------------------

--------------结果输出-------------------- -结果输出--------------------------

Since I am new, I need a 10 reputation to post pictures of the output...so here is the output by typing it out...

因为我是新手,所以我需要 10 个声望来发布输出的图片......所以这是通过输入输出......

//this is for if I enter String "e"

//这是为了如果我输入字符串“e”

Enter grade (one character): e Invalid grade - must enter A,B,C,D,F (upper or lower case)

输入成绩(一个字符): e 成绩无效 - 必须输入 A、B、C、D、F(大写或小写)

false

错误的

false

错误的

//this is for if I enter String "A"

//这是为了如果我输入字符串“A”

Enter grade (one character): A

输入等级(一个字符):A

Invalid grade - must enter A,B,C,D,F (upper or lower case)

成绩无效 - 必须输入 A、B、C、D、F(大写或小写)

true

真的

The question***:For my output,when I enter "e", I was hoping to see something like

问题***:对于我的输出,当我输入“e”时,我希望看到类似

Enter grade (one character): e

输入等级(一个字符):e

Invalid grade - must enter A,B,C,D,F (upper or lower case)

成绩无效 - 必须输入 A、B、C、D、F(大写或小写)

false

错误的

false

错误的

//loops through

//循环

Enter grade (one character):

输入等级(一个字符):

So I was hoping that when I entered "e" it would make it false...and then start at the beginning until a result finally is entered that is: A,B,C,D,F (upper or lower case)

所以我希望当我输入“e”时它会使它成为假......然后从头开始直到最终输入结果:A,B,C,D,F(大写或小写)

What is my mistake? I have been trying to brainstorm it myself, but I thought I would see what you guys have to say....

我的错误是什么?我一直试图自己集思广益,但我想我会看看你们要说什么......

If you think the strategy I am using to get the output is not the best....What strategy/logic would you recommend?

如果您认为我用来获得输出的策略不是最好的……您会推荐什么策略/逻辑?

Thanks for the help!

谢谢您的帮助!

采纳答案by Jon Kiparsky

 while(x=false);

This is wrong. (should be ==)

这是错误的。(应该是==)

回答by CoderCroc

Yes =is assignment operator you have to use ==to check equality and other than that,note here your switch can be reduce to following and currently you have redundant code you can better use if statement with ||operator here.

是的,=您必须使用赋值运算符==来检查相等性,除此之外,请注意,此处您的 switch 可以简化为跟随,目前您有多余的代码,您可以||在这里更好地使用 if 语句与运算符。

  switch (letter) {
     case "a": 
     case "A":    
     case "b":
     case "B":
     case "c":
     case "C":    
     case "d":   
     case "D":
     case "f":     
     case "F":
       x=true;
       break;
     default:
     //...

回答by Donal

You can simplify the switch statement even further. By converting the letter to upper case you are checking for both upper and lower case. For example:

您可以进一步简化 switch 语句。通过将字母转换为大写,您可以同时检查大写和小写。例如:

    do{
        System.out.print("Enter grade (one character): ");
        letter = sc.next(); 
        switch (letter.toUpperCase()) {
            case "A":                   
            case "B":       
            case "C":       
            case "D":       
            case "F":
                x=true;
                break;
            default:
                System.out.println("Invalid grade - must enter A,B,C,D,F (upper or lower case)");                    
                break;
        }                    
    } while(x==false);        

    System.out.println(x);