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.

class Questions

{

//--Main Method--

public static void 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();

}

static void 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();

}

static void 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

}

static void 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

}

static void 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

}

}

}

[2929 byte] By [student_techiea] at [2007-11-27 6:11:48]
# 1

> I can't seem to get this to work.

Why not? A bit more description would be helpful - like whether it compiles or not, and, if it does, what happens when you run the program.

> static void 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

> }

This can't be right. You declare option, then eventually assign it a value from user input, and then throw that value away. option is a local variable so when the method ends it effectively disappears. You should consider the fact that methods can return values. If option is going to be used to control whether the program ends or not it will have to be returned.

(Use the "code" tags when you post code. They are described here http://forum.java.sun.com/help.jspa?sec=formatting and will help keep your code readable when it appears here.)

pbrockway2a at 2007-7-12 17:18:21 > top of Java-index,Java Essentials,Java Programming...