我是java新手,这是我开发的第一个程序。基本上,您输入“1”,即进入游戏。
将级别设置为 1。
然后,它将根据级别选择一个问题。第一轮工作正常。在我在第 1 轮正确回答后,它让我进入第 2 题。
但在第 2 题中,如果我回答正确,它将坚持第 2 题并重复,而不是转到下一个问题。
为什么这样做?我有级别++?
public static void main (String[] args) {
gameRoom();
}
public static void gameRoom() {
System.out.println("Welcome to the Truth and False game!");
System.out.println("Please write 1 to start, or -1 to exit program.");
InputI = console.nextInt();
if(InputI == 1) {
inGame = true;
gameProcess(1);
}
}
public static void gameProcess(int level) {
switch (level) {
case 1:
question = tof.getQuestion(1);
System.out.println("Your question is: " + question);
System.out.println("Please answer, true or false!.");
level = 1;
break;
case 2:
question = tof.getQuestion(2);
System.out.println("Your question is: " + question);
System.out.println("Please answer, true or false!.");
level = 2;
break;
case 3:
System.out.println("Hey1");
break;
}
while (inGame) {
InputS = console.nextLine();
while (InputS != "") {
inGame = false;
InputS = console.nextLine();
checkQuestion(level, InputS);
}
}
}
public static void checkQuestion (int level, String question)
{
Boolanswer = tof.checkQuestion(level, question);
level++;
if (Boolanswer) {
System.out.println("Correct!" + correctAnswers);
correctAnswers++;
} else {
System.out.println("Incorrect! " + correctAnswers);
}
inGame = true;
gameProcess(level);
}
问题可能是,系统在输入!=“”时无法读取循环,但我可能错了,这是另一个类:
private String[][] questionsArray = new String[][]
{
{"Is sky blue?", "true"},
{"Is sky green?", "false"}
};
private String answer = "";
private String Currentanswer = "";
private String question = "";
private int level = 0;
private boolean returnValue = false;
public String getQuestion(int Level) {
switch (Level) {
case 1:
question = questionsArray[0][0];
break;
case 2:
question = questionsArray[1][0];
break;
}
return question;
}
public String getAnswer(int Level) {
switch (Level) {
case 1:
answer = questionsArray[0][1];
break;
case 2:
answer = questionsArray[1][1];
break;
}
return answer;
}
public boolean checkQuestion(int level, String answer) {
switch (level) {
case 1:
Currentanswer = questionsArray[0][1];
if (Currentanswer.equalsIgnoreCase(answer)) {
returnValue = true;
} else {
returnValue = false;
}
break;
case 2:
Currentanswer = questionsArray[1][1];
if (Currentanswer.equalsIgnoreCase(answer)) {
returnValue = true;
} else {
returnValue = false;
}
break;
}
return returnValue;
}
问题是,当我正确回答第二个问题时,它什么也不会打印。如果我回答不正确,它将转到第一个问题。
我只有两个问题,我知道,但我只是在测试。级别应该达到 3,并打印“嘿!” 根据开关情况。