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

[7216 byte] By [student_techiea] at [2007-11-27 6:13:41]
# 1

Please in future refrain in multiposting http://forum.java.sun.com/thread.jspa?threadID=5179108

Also, while you have now used code tags, and thanks for doing so, you haven't addressed any of the problems raised by that previous thread.

Namely. The option variable needs to be a class variable (and static by the way you have written this) and not a local variable to each method.

As you have it right now you set a variable named option in your next method and promptly throw it away. Then you create another local variable named option inside your loop method. These are not the same variables!

cotton.ma at 2007-7-12 17:22:26 > top of Java-index,Java Essentials,Java Programming...
# 2

To make this as clear as possible

static void next()

{

int option = 0; // this method is LOCAL to next

System.out.println("Press 1 to exit, 2 to continue."); //gives to option tocontinue or to quit the program

option = KeyIn.anInt(); //user inputs what they want to do

}

static void loop()

{

int option = 0; // this method is local to loop. this is NOT the same variable as the one from next

while (option == 2)

{

question(); //if the number is 2 then it will continue with anotherquestion

}

break; // why?

System.out.println("End program"); //if number =1 will exit program

}

cotton.ma at 2007-7-12 17:22:26 > top of Java-index,Java Essentials,Java Programming...