Reputation: 43
I am new to java, this is my first program I've been developing. Basically, you enter '1', and that enters you to the game.
Sets level to 1.
Then, it will select a question, based on the level. First round works fine. After I answer correctly on round 1, It moves me to question 2.
But in question 2, if I answer correctly, it will stick on question 2 and repeat it instead of moving to the next question.
Why is it doing that? I have level++?
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);
}
The problem might be that, the system can't read the loop while input != "", I may be wrong though, this is the other class:
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;
}
Problem is, when I answer correctly, the second question, it will print nothing. and if I answer not correctly, it will move to the FIRST question.
I only have 2 questions, I know, but I am just testing. Level should go to 3, and print "Hey!" according to the switch cases.
Upvotes: 2
Views: 482
Reputation: 8938
You don't support level > 2
public String getQuestion(int Level) {
switch (Level) {
case 1:
question = questionsArray[0][0];
break;
case 2:
question = questionsArray[1][0];
break;
}
return question;
}
Your question = questionsArray[1][0]; stays for any level other than 1 or 2.
If you send level 3 then switch does nothing and previos question is returned,
Upvotes: 0
Reputation: 5684
The problem might be in this loop:
while (inGame) {
InputS = console.nextLine();
while (InputS != "") {
inGame = false;
InputS = console.nextLine();
checkQuestion(level, InputS);
}
}
Try to replace while (InputS != "")
with while (!InputS.equals("")) {
Upvotes: 1