For example, I’m asking whether or not a person wants an ice-cream. If they say yes, (or input 1) I want it to ask the next question, “Which flavour would you like?”, but if they say no, I want it to skip the next question all together and go onto whatever question is after that one that was voided, such as “Do you like toast?”
I tried using if statements, but didn’t have that much luck.
I basically wrote:
if (answer == 1) {
System.out.print("Which flavour would you like?");
flavour = keyboard.nextLine;
}
else {}
But the compiler gets annoyed because I use the value flavour further down in the code and it seems like because it’s in an if statement the program is having difficulty accessing it. (Because further down in the code I ask questions based on which ice cream flavour the person chose.
Thanks greatly.
The problem is that
flavouris not set to anything if theansweris not1. What do you expect it to do?To fix this, you need to value flavour a value either before the
ifor in theelseblock. You may need to check this later to see if its sensible.