how to set a switch statement in while loop in java(如何在java的while循环中设置switch语句)
本文介绍了如何在java的while循环中设置switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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 statement does not break. Please help
public static void main(String[] args) throws IOException {
// start both with 1 point
int goodTotal = 50;
int monTotal = 50;
// input switch statement
while (goodTotal > 0 && monTotal > 0) {
System.out.print("Type a letter: ");
System.out.println("
");
System.out.print("F: Go out and Fight ");
System.out.println("
");
System.out.print("R: Rest ");
System.out.println("
");
System.out.print("C: Check Stats ");
System.out.println("
");
System.out.print("Q: Quit ");
int input = System.in.read();
System.out.println("You typed: " + (char) input);
switch (input) {
case 'f':
System.out.println("Continue the game");
break;
case 'r':
System.out.println("Players should rest");
break;
case 'c':
System.out.println("Checking the status of the game");
System.out.print("Goodman has " + goodTotal + " points and Monster has " + monTotal + " points");
System.out.println("
");
break;
case 'q':
System.out.println("Game over");
System.exit(input);
break;
default:
System.out.println("Invalid selection");
break;
}
// Set value of minimum and maximum damage
int minDmg = 2;
int maxDmg = 15;
// Get random number;
int damage = minDmg + Double.valueOf(Math.random() * (maxDmg - minDmg)).intValue();
int damage2 = minDmg + Double.valueOf(Math.random() * (maxDmg - minDmg)).intValue();
// remove value of damage from started value to get total value remaining
goodTotal = goodTotal - damage;
monTotal = monTotal - damage2;
// print message if still in the game
if (goodTotal > 0) {
System.out.println("Goodman has " + goodTotal + " points left. Not bad, Man! ");
}
// if Goodman survives round 2 print a message of encouragement
if (goodTotal > 0 && count > 1 && count <= 2) {
System.out.print("This is encouraging. Goodman has lasted past roundhh " + count + ". ");
// print new message if Goodman passes round 3
} else if (goodTotal > 0 && count == 3) {
System.out.print("Goodman is as strong as Samson. He has lasted round " + count
+ " and still looks strong.");
System.out.print(" 10 hit points has been added to your total");
}
if (monTotal > 0) {
System.out.println("Wait, Monster has a total of " + monTotal + " points and is still in the game");
}
// exit if have less than 0 point, and print game over. Congratulate the winner
if (goodTotal < 0) {
System.out.println("Goodman you are out of the game");
System.out.println("The monster will take over the village. This is sad");
System.out.println("Game Over!");
} else if (monTotal < 0) {
System.out.println("Goodman has been victorious");
System.out.println("The monster is dead. The people live!!!!");
System.out.println("Game Over!");
}
System.out.println("This is the end of round " + count + " ");
System.out.println("
");
count = count + 1;
}
}
解决方案
Use a label on the loop:
loop: while (goodTotal > 0 && monTotal > 0) {
// ...
switch (input) {
case 'f':
// ...
break loop;
case 'r':
// ...
break loop;
// ...
}
// ...
}
这篇关于如何在java的while循环中设置switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何在java的while循环中设置switch语句


基础教程推荐
猜你喜欢
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01