java 中的循环开关盒
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);
我认为一个简单的解决方案是首先提示用户键入有多少学生,然后将您的 switch 语句放入 for 循环中。
假设您只希望循环在 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);
所以我尝试使用 for 循环方法,感谢 Andreas 举了一个例子:),它成功了!
现在我不知道如何提示用户再次输入代码以继续循环,但我输入了几行代码来提示代码并且它按预期工作,但我确定这不是最有效的怎么做?
我打算在用户输入最后一个 input/entered 无效代码后打破循环
这是修改后的新代码
循环:对于 (;;){
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;
}
}
我目前正在研究将提示学生级别输入代码(切换大小写)的代码,并确定每种类型的学生将收到的现金数额。然后我想总结每个学生级别收到的所有现金。我是一个初学者,只学习 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);
我认为一个简单的解决方案是首先提示用户键入有多少学生,然后将您的 switch 语句放入 for 循环中。
假设您只希望循环在 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);
所以我尝试使用 for 循环方法,感谢 Andreas 举了一个例子:),它成功了!
现在我不知道如何提示用户再次输入代码以继续循环,但我输入了几行代码来提示代码并且它按预期工作,但我确定这不是最有效的怎么做?
我打算在用户输入最后一个 input/entered 无效代码后打破循环
这是修改后的新代码
循环:对于 (;;){
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;
}
}