•
•
•
•
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
![]() |
•
•
Join Date: Aug 2004
Posts: 11
Reputation:
Rep Power: 5
Solved Threads: 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.
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.
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?
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
•
•
Join Date: Aug 2004
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 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 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();
}
}
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();
}
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
access activation api blogger blogging blogs code com combo dani daniweb data debugging development dreamweaver dropdownlist gdata google gpl html innovation java key linux microsoft microsoft sdk for java microsystems module net news open openbsd platform product programming reuse rss serial software source sun tags vista web wysiwyg xml
- Using loop functions (C)
- can anyone help me with this progam,and write the code for this problem? (C)
- How would I write this asp code to work in PHP (PHP)
- How to write demo code for IVR (VB.NET)
- Can we Programaticall Write code in VB6.0, forms/standard module/class code window (Visual Basic 4 / 5 / 6)
- Help with writing a bit of code (C)
- Writing a loop statement that produces desired output (Java)
- pointer/reference ? issues ... need help with simple code (C++)
- Doesn't check if username or email exits in database properly (ASP.NET)
- my compliler seems to be ignoring my while loop (Java)
Other Threads in the Java Forum
- Previous Thread: Ms Access sql problem
- Next Thread: Mock exam help?


Linear Mode