User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,530 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,868 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4450 | Replies: 4
Reply
Join Date: Aug 2004
Posts: 11
Reputation: srthomso is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
srthomso srthomso is offline Offline
Newbie Poster

I need to re-write this code using a 'while' loop.

  #1  
Aug 10th, 2004
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2003
Location: Malaysia
Posts: 313
Reputation: red_evolve is on a distinguished road 
Rep Power: 6
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: I need to re-write this code using a 'while' loop.

  #2  
Aug 11th, 2004
Greetings.
What would you like to do in order to trigger the loop?
When the input given is none other than "play" or "end" ?
"Study the past if you would define the future" - Confucius
Reply With Quote  
Join Date: Aug 2004
Posts: 11
Reputation: srthomso is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
srthomso srthomso is offline Offline
Newbie Poster

Re: I need to re-write this code using a 'while' loop.

  #3  
Aug 11th, 2004
Originally Posted by red_evolve
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
Reply With Quote  
Join Date: Jun 2003
Location: Malaysia
Posts: 313
Reputation: red_evolve is on a distinguished road 
Rep Power: 6
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: I need to re-write this code using a 'while' loop.

  #4  
Aug 11th, 2004
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();
}
"Study the past if you would define the future" - Confucius
Reply With Quote  
Join Date: Aug 2004
Posts: 4
Reputation: bvvkrishna is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bvvkrishna bvvkrishna is offline Offline
Newbie Poster

Re: I need to re-write this code using a 'while' loop.

  #5  
Aug 12th, 2004
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 bvvkrishna@rediffmail.com
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();
}
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 4:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC