我是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,打印“嘿!”根据转换案例。
答案 0 :(得分:1)
问题可能出在这个循环中:
while (inGame) {
InputS = console.nextLine();
while (InputS != "") {
inGame = false;
InputS = console.nextLine();
checkQuestion(level, InputS);
}
}
尝试将while (InputS != "")
替换为while (!InputS.equals("")) {
答案 1 :(得分:0)
您不支持关卡> 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,则交换机不执行任何操作,并返回优先级问题,