【问题标题】:Looping switch case in javajava中的循环开关盒
【发布时间】:2021-02-07 09:12:18
【问题描述】:

我目前正在编写代码,该代码将提示学生级别的代码类型(switch case),并确定每种类型的学生将收到的现金金额。然后我想总结每个学生级别收到的所有现金。我是一个初学者,只学了几个星期的java,任何建议都非常感谢。这是我的工作,也有注释。

System.out.print("Level code :");
levelCode = input.next().charAt(0);

do {

    switch (levelCode) {
        case 'F':
            System.out.print("Total Foundation student = ");
            studentFoundation = input.nextInt();
            foundationCash = studentFoundation * 50;
            break;
        case 'D':
            System.out.print("Total Diploma student = ");
            studentDiploma = input.nextInt();
            diplomaCash = studentDiploma * 50;
            break;
        case 'B':
            System.out.print("Total Bachelor student = ");
            studentBachelor = input.nextInt();
            bachelorCash = studentBachelor * 70;
            break;
        case 'M':
            System.out.print("Total Master student = ");
            studentMaster = input.nextInt();
            masterCash = studentMaster * 90;
            break;
        case 'P':
            System.out.println("Total PhD student = ");
            studentPhd = input.nextInt();
            phdCash = studentPhd * 90;
            break;
        default:
            System.out.println("Invalid code");
            break;

    }
} while (levelCode < 5); //i dont know what to put here in order to loop the switch case

totalCash = foundationCash + diplomaCash + bachelorCash + masterCash + phdCash; //here i want to calculate all the input entered

System.out.println("Total cash amount = " + totalCash);

【问题讨论】:

  • 您好,请问。首先你想如何结束循环?想输入多少后?假设您只接受 10 个输入?或者可能直到输入特定的键?就像您接受输入,但直到有人按“E”退出?
  • 一旦用户选择默认值,我想结束循环,是的,像 E 这样的退出

标签: java loops switch-statement


【解决方案1】:

我认为一个简单的解决方案是首先提示用户输入有多少学生,然后将您的 switch 语句放入 for 循环中。

【讨论】:

  • 谢谢。我已经尝试过了,但在这种情况下,我不确定如何循环开关盒,因为我尝试了很多我在网上找到的方法。
【解决方案2】:

假设您只希望在执行 default 案例时让循环返回,您可以通过不同的方式执行此操作。

使用标签

LOOP: for (;;) { // forever loop
    System.out.print("Level code :");
    levelCode = input.next().charAt(0);

    switch (levelCode) {
        case 'F':
            // code here
            break LOOP; // <=== break out of the loop, not the switch statement
        case 'D':
            // code here
            break LOOP; // <=== break out of the loop, not the switch statement
        ...
        default:
            // code here
    }
}

使用布尔值

boolean error;
do {
    System.out.print("Level code :");
    levelCode = input.next().charAt(0);

    error = false;
    switch (levelCode) {
        case 'F':
            // code here
            break;
        case 'D':
            // code here
            break;
        ...
        default:
            error = true;
            // code here
    }
} while (error);

使用特殊值,例如空

String levelCode;
do {
    System.out.print("Level code :");
    levelCode = input.next();

    switch (levelCode) {
        case "F":
            // code here
            break;
        case "D":
            // code here
            break;
        ...
        default:
            // code here
            levelCode = null;
    }
} while (levelCode == null);

【讨论】:

  • 非常感谢您。很抱歉,我没有提到我想在用户选择所有其他代码时循环 switch case,并在输入默认值后打破循环,总结所有输入。所以我相信我必须改变循环的条件才能反过来发生?
【解决方案3】:

所以我尝试使用 for 循环方法,感谢 Andreas 提供的示例 :),它成功了!

现在我不知道如何提示用户再次输入代码以继续循环,但我已经输入了几行来提示代码并且它按预期工作,但我确定这不是最有效的方法干吗?

我打算在用户输入最后一个输入/输入无效代码后打破循环

这是修改后的新代码

循环:for (;;){

    switch(levelCode){
     case 'F' : System.out.print("Total Foundation student = ");
                studentFoundation = input.nextInt();
                foundationCash = studentFoundation * 50;
                
                System.out.print("Level code :");//here im trying to prompt the user to enter another code to continue the loop
    levelCode = input.next().charAt(0);
    break;
     case 'D' : System.out.print("Total Diploma student = ");
                studentDiploma = input.nextInt();
                diplomaCash = studentDiploma * 50;
                
                System.out.print("Level code :");
    levelCode = input.next().charAt(0);
    break;
     case 'B' : System.out.print("Total Bachelor student = ");
                studentBachelor = input.nextInt();
                bachelorCash = studentBachelor * 70;
                
                System.out.print("Level code :");
    levelCode = input.next().charAt(0);
    break;
     case 'M' : System.out.print("Total Master student = ");
                studentMaster = input.nextInt();
                masterCash = studentMaster * 90;
                
                System.out.print("Level code :");
    levelCode = input.next().charAt(0);
    break;
     case 'P' : System.out.println("Total PhD student = ");
                studentPhd = input.nextInt();
                phdCash = studentPhd * 90;
                break LOOP;
                
      default : System.out.println("Invalid code :");
                break LOOP;
                
               
    }      
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多