[英]Looping switch case in java
我目前正在研究将提示学生级别类型的代码(切换大小写),并确定每种类型的学生将收到的现金数额。 然后我想总结每个学生级别收到的所有现金。 我是一个初学者,只学了几个星期的 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);
假设您只希望在执行default
情况时返回到 go 的循环,您可以通过不同的方式进行。
使用 label
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
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);
使用特殊值,例如 null
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 语句放入 for 循环中。
所以我尝试使用 for 循环方法,感谢 Andreas 举了一个例子:),它成功了!
现在我不知道如何提示用户再次输入代码以继续循环,但我输入了几行来提示代码并且它按预期工作,但我确定这不是最有效的方法吗?
我打算在用户输入最后一个输入/输入无效代码后打破循环
这是新修改的代码
循环:对于(;;){
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.