Java 初学者你好!我目前正在做一个简单的测验/游戏节目,同时使用 While 循环和 If 语句来学习经验。不幸的是,我陷入了对它实施一个健康点系统的困境,因为 if 语句负责在 HP 降低太多时抢先停止循环,无法读取 HP 降低的任何内容,以及据称检测到错误答案并使 HP 的语句减少不知何故不起作用。这是我的代码的基本格式:
int Value = Number;
while(true)
//or
while(Value == Number) {
//Question and Answer String
System.out.println("Question");
String Input = Scanner.nextline();
//To check if the Answer is right or wrong
while(true) {
//Answer is Correct
if (Condtion) {
System.out.println("Something");
break;
//Answer is Wrong
} else {
System.out.println("Something");
//Reduce the HP Here
Value -= 1;
break;
}
}
//Question is repeated 5 times
//Question is continuous even if one question got wrong answer but resets after 3 tries
//If Statement for Losing all HP
// Ask to either quit playing or reset to the first question
if(Condition to stop the loop the moment HP decreases to zero) {
System.out.println("Something");
String Input = Scanner.nextline();
//Answer is No
if(Condition) {
//Answer is Yes
} else if (Condition) {
//Repeat Question Format
//Answer is something else
] else {}
}
}
//After answering all questions once, statement appears if you want to continue playing or not
System.out.println("Something");
String Input = Scanner.nextline();
//Answer is No
if(Condition) {
//Answer is Yes
} else if (Condition) {
//Answer is something else
] else {}
如果我的代码令人困惑,我将发布我用来更好地可视化代码中的错误的实际代码行。
“请发布一个最小的可重现示例。” – 安迪·特纳
感谢您的回复,安迪!很抱歉,在将代码发布到此处之前,我没有清理代码。所以,这里有一个新的、更干净的版本(我现在还指出了哪些代码给我带来了麻烦)。谢谢大家对我的帮助!
import java.util.Scanner;
public class Testing {
public static void main(String[] args) {
Scanner Scanner = new Scanner(System.in);
int Value = 3;
while(true) {
//Question and Answer String, example
System.out.println("Question: 3 + 2 = 5, True or False?");
String Input = Scanner.nextLine();
//To check if the Answer is right or wrong
while(true) {
//Answer is Correct
if (Input.equals("True")) {
System.out.println("Correct!");
break;
//Answer is Wrong
} else {
System.out.println("Wrong!");
//Reduce the HP Here (Problem)
Value -= 1;
break;
}
}
//Q&A should repeat 5 times
//Q&A still continues even if its wrong but resets after 3 tries
//If Statement for Losing all HP
//Asks either quit playing or reset to the first question (Problem)
if(Value == 0) {
System.out.println("Lose. Try again?");
String Input2 = Scanner.nextLine();
//Answer is No, (Input2 for distinction)
if(Input2.equals("No")) {
System.out.println("Thanks for Playing!");
//Answer is Yes
} else if (Input2.equals("Yes")) {
//Repeat Question Format
Input = Scanner.nextLine();
//Answer is something else
} else {
System.out.println("What? Please answer again.");
}
//After answering all questions once, statement appears if you want to continue playing or not
System.out.println("Want to keep playing?");
String Input3 = Scanner.nextLine();
//Answer is No
if(Input3.equals("No")) {
System.out.println("Thanks for Playing!");
//Answer is Yes
} else if (Input3.equals("Yes")) {
//Repeat Question Format
Input = Scanner.nextLine();
//Answer is something else
} else {
System.out.println("What? Please answer again.");
}
}
}
}
}