While Loops
I have this program, and I when I press 1 I want the program to quit, and 2 to continue, I can't seem to get this to work. It complies fine, and it will run, but I can't get the press 1 to exit and press 2 to continue to work. For some reason I need to put option=0 because it says variable not initialized if I don't
class Questions
{
// --Main Method--
publicstaticvoid main(String[]args)
{
// --Title--
System.out.println("Ask any question, and get an answer!");//introduces the program
System.out.println("Ask your question:");
question();
loop();
}
staticvoid question()
{
// --Variable Declaration and Initialization--
String question;
// --Requests Input--
question = KeyIn.aString();//asks the user to input a question
int k = EasyRandom.getInt(10);//randomly picks an answer
answer(k);//outputs the answer
// --Output--
next();
}
staticvoid next()
{
int option = 0;
System.out.println("Press 1 to exit, 2 to continue.");//gives to option to continue or to quit the program
option = KeyIn.anInt();//user inputs what they want to do
}
staticvoid loop()
{
int option = 0;
while (option == 2)
{
question();//if the number is 2 then it will continue with another question
}
break;
System.out.println("End program");//if number =1 will exit program
}
staticvoid answer(int k)
{
switch(k)
{
case 1: System.out.println("Don't count on it");break;//outputs answer
case 2: System.out.println("I don't think so");break;//outputs answer
case 3: System.out.println("Perhaps");break;//outputs answer
case 4: System.out.println("It doesn't look good");break;//outputs answer
case 5: System.out.println("It must be your lucky day");break;//outputs answer
case 6: System.out.println("Absolutely");break;//outputs answer
case 7: System.out.println("Ask again later");break;//outputs answer
case 8: System.out.println("Things look unclear right now");break;//outputs answer
case 9: System.out.println("Without a doubt");break;//outputs answer
case 10: System.out.println("Probably not");break;//outputs answer
case 11: System.out.println("Not looking good");break;//outputs answer
case 12: System.out.println("Unlikely");break;//outputs answer
case 13: System.out.println("Without a doubt");break;//outputs answer
case 14: System.out.println("Of course");break;//outputs answer
case 15: System.out.println("It depends");break;//outputs answer
case 16: System.out.println("Yes");break;//outputs answer
case 17: System.out.println("It will happen in the near future");break;//outputs answer
case 18: System.out.println("If you believe in it");break;//outputs answer
case 19: System.out.println("Don't worry");break;//outputs answer
case 20: System.out.println("Not a problem");break;//outputs answer
}
}
}
Message was edited by:
student_techie

