![](/img/trans.png)
[英]setReadable(true) doesn't work after setReadable(false)
[英]True or false game doesn't work after 1st round
我是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,并打印“嘿!” 根据开关情况。
问题可能出在以下循环中:
while (inGame) {
InputS = console.nextLine();
while (InputS != "") {
inGame = false;
InputS = console.nextLine();
checkQuestion(level, InputS);
}
}
尝试将while (InputS != "")
替换为while (!InputS.equals("")) {
您不支持级别> 2
public String getQuestion(int Level) {
switch (Level) {
case 1:
question = questionsArray[0][0];
break;
case 2:
question = questionsArray[1][0];
break;
}
return question;
}
您的问题= questionsArray [1] [0]; 停留在1或2以外的任何级别。
如果您发送第3级,则switch不执行任何操作,则返回previos问题,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.