0

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.

Edited by pyTony: fixed formating

3
Contributors
4
Replies
5
Views
12 Years
Discussion Span
Last Post by bvvkrishna
0

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

0

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

0

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();
}
0

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();
}
}

Edited by Nick Evan: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.