I've been trying to make a choose your adventure type of program, but I've run into a problem. I have the whole thing running in a do while loop, and each option is an element in an array is an element. I also have if statements in the do while that can change some elements depending on what the user has already done. Here's an example of what my code looks like:
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner input;
input=new Scanner(System.in);
boolean run;
boolean theBoo = false;
run = true;
int choice;
choice = 0;
do {
String[] theArray;
theArray = new String[2];
theArray[0] = "Hello";
if(theBoo){
theArray[1] = "Goodbye";
}
else{
theArray[1] = "Hi";
theBoo = true;
}
System.out.println(theArray[choice]);
choice = input.nextInt();
} while(run);
}
}
For some reason though, it prints out "Goodbye" if you input 1, even though it should print "Hi", as theBoo is false. My question is: why is the do while loop changing the value of my variable, and how do I prevent it from doing so? Thanks!
Edit: Btw, I am new here, so my apologies if I did something wrong.
Edit2: First of all, thanks everyone for the quick answers. I made the changes you recommended, but its still doing the same thing. I updated the code to what it is with the changes.
==for comparisons.if(boolean == true). It is fairly obvious that theifstatement evaluates abooleanso it should be obvious thatif(boolean)is the correct construction. This also has be nice feature of preventing theif(boolean = true)which is what we have here.