code:

public static void menu(){
    String reply;
    System.out.println("Make your choice:");
    reply = getLineFromUser(); //assume this function works correctly
    if(reply.equals("play")){
         playgame();
    }
    else
        if(reply.equals("end")){
           stopGame();
        }
   menu(); // recursive call. Don't use!!!!!    

Any ideas? i struggle with while loops at the best of times.

Recommended Answers

All 4 Replies

Greetings.
What would you like to do in order to trigger the loop?
When the input given is none other than "play" or "end" ?

Greetings.
What would you like to do in order to trigger the loop?
When the input given is none other than "play" or "end" ?

I dont quite understand wot u mean? Sorry i'm new to all this language malarky.

Ste

Greetings.
Erm, okay, sorry for that.
You said you'd want to create a while loop?
So my question was, what do you want that while loop to do?
Keep prompting for an input (in your case, choice) from the user when the input is erroneous? If so, then is it something like this?

public static void menu()
{
  String reply;
  
  do{
    System.out.println("Make your choice:");
    reply = getLineFromUser(); //assume this function works correctly
  }while(reply.equals("play")==false || reply.equals("end") == false);

  if(reply.equals("play"))
    playgame();
  
  else if(reply.equals("end"))
    stopGame();
}

the loop will be terminated when reply is assigned "stop".
to enter into the loop initially we've to assign any value other than "stop". so i assinged it as "start";
still if u got any doubts about this code. write to me at <<snip>>
code:

public static void menu()
{


String reply="start";//"start" assigned to reply to enter into the loop


while(reply.equals("stop")==false) {
//terminates when reply is assigned "stop"


System.out.println("make ur choice : ");
reply=getLineFromUser();
if(reply.equals("play")==true)
playGame();
else if(reply.equals("end")==true)
stopGame();
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.