KobiF KobiF - 2 months ago 7
Java Question

Java While Loop Not Ending

I am very new to java and decided to start a new project recently to help myself learn. My project is an impossible quiz. I started making my first question in the quiz and ran into a problem. I have a while loop that is supposed to exit the question if the player runs out of lives however it is not working. The while loop looks like:

while (lives != 0 || !correct == true) {


package mainpackage;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class Main {

public static void main(String[] args) {

int totallevels = 1;
int currentlevel = 1;
int startcounter = 0;
int skips = 1;
int lives = 3;
boolean correct = false;

String choice;
Scanner begin = new Scanner(System.in);

System.out.println("Welcome to the Impossible Quiz!!");

do {

System.out.println("Type -start- to start the game or -instructions- to see the games instructions");

choice = begin.nextLine();

switch (choice) {
case "start":
System.out.println("The game will begin in");

try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
}

System.out.println("3");

try {
TimeUnit.MILLISECONDS.sleep(1500);
} catch (InterruptedException e) {
}

System.out.println("2");

try {
TimeUnit.MILLISECONDS.sleep(1500);
} catch (InterruptedException e) {
}

System.out.println("1");

startcounter = 1;

break;
case "instructions":
System.out.println("INSTRUCTIONS:");

try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
}

System.out.println("The game is very simple.");

try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
}

System.out.println("There are " + totallevels + " levels.");

try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
}

System.out.println("In each level you will be presented with a question that you must answer in order to move on to the next one.");

try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
}

System.out.println("When you think you have the answer all you have to do is type the letter corrosponding to your answer.");

try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
}

System.out.println("Each game you get 1 skip which alows you to skip the level you are on.");

try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
}

System.out.println("You also get 3 lives.");

try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
}

System.out.println("If you run out of lives you will have to restart the game.");

try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
}

break;
}

} while (startcounter != 1);

currentlevel = 1;

//
//LEVEL ONE
//
while (lives != 0 || !correct == true) {

try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
}

System.out.println(" ");
System.err.print("Level: ");
System.out.print(currentlevel);
System.out.println(" ");

try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
}

System.err.print("Lives: ");
System.out.println(lives);

try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
}

System.err.print("Skips: ");
System.out.println(skips);

try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
}

System.out.println(" ");
System.out.println("Question: If there are 6 apples in a tree and you take 4, how many do you have?");

try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
}

System.out.println("A: 3");
System.out.println("B: 4");
System.out.println("C: 2");
System.out.println("D: 6");

while (!choice.equalsIgnoreCase("b")) {

Scanner questionone = new Scanner(System.in);
choice = questionone.nextLine();

switch (choice) {
case "a":
System.out.println("WRONG! Try again.");
lives = lives - 1;
break;
case "b":
System.out.println("CORRECT! You have the 4 you took obviously.");
correct = true;
break;
case "c":
System.out.println("WRONG! Try again.");
lives = lives - 1;
break;
case "d":
System.out.println("WRONG! Try again.");
lives = lives - 1;
System.out.println(lives);
break;
default:
System.out.println("Please type an answer.");
break;
}
}
}

System.out.println("test");

//
//LEVEL TWO
//
}
}


PS. Yes I have spent a lot of time trying to figure it out on my own and also seeing if anyone else has had the same problem.

Answer

I think your problem is here:

while (lives != 0 || !correct == true) {

You stop your while when your correct = true OR your lives = 0

It will also mean: You continue your while when your correct = false AND your lives != 0.

So change your condition to

while (lives != 0 && !correct == true) {

And more, the condition of the while:

while (!choice.equalsIgnoreCase("b")) {

You see, your while loop ( the inside one ) will ONLY stop when you input "b" from your keyboard. So that when you press a ( you lost 1 live ), and a a a a ..etc, your lives will be negative number.

So change to:

while (!choice.equalsIgnoreCase("b") && lives != 0) {

to check whether you have live or not.

Good luck!