aspose file tools*
The moose likes Beginning Java and the fly likes Restart a while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Restart a while loop" Watch "Restart a while loop" New topic
Author

Restart a while loop

Vlad Ivanov
Greenhorn

Joined: Mar 02, 2010
Posts: 7
So here's the scoop. My final project for my java class is to make a game. My group decided to do a text-based adventure game but I need some help on a section.



As you can see, if the player doesn't input any of the options, the loop terminates, and in this case the enemy gets 2 free attacks. I want to be able to print a line that says "Command not recognized" (I can do that part), and then return to the top of the loop so they can try again (don't know how to do this part).

Thank you!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You may wish to use "else" and "else if" in your code.
Vlad Ivanov
Greenhorn

Joined: Mar 02, 2010
Posts: 7
For the purpose of my code, the else-if statements wouldn't accomplish what I'm trying to do. I need to start the loop over so they have the chance to enter a command again.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Vlad --

I'm confused; what you're looking for is the very definition of a while loop. When it gets to the bottom, it goes back to the top; it does not terminate unless there's a "break" or "return", or the loop condition becomes false. It should already behave exactly as you want it to.


[Jess in Action][AskingGoodQuestions]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

Ernest Friedman-Hill wrote:Vlad --

I'm confused; what you're looking for is the very definition of a while loop. When it gets to the bottom, it goes back to the top; it does not terminate unless there's a "break" or "return", or the loop condition becomes false. It should already behave exactly as you want it to.


Yes the while loop should continue to execute until the player is dead or the enemy is dead. Or until run is typed.


Visit my blog! http://jameshambrick.com
Vlad Ivanov
Greenhorn

Joined: Mar 02, 2010
Posts: 7
Correct that the while loop will keep running until the enemy or player is dead. The caveat lies in the last "if" statement. After the player has a chance to attack(), run(), or drinkPotion(), the enemy automatically attacks. But say the player enters: "dance" as a command. The command isn't recognized as any of the three, so the enemy attacks and the player loses one turn. The next time, the player enters "jump," but again, it isn't recognized as a command so the monster gets another free attack. this could continue indefinitely until the player is dead. I need a way to catch unrecognized input and say "No! You have to enter one of the three commands!" Before returning them to the top of the loop so they can try again. Does that clarify?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Ah, OK, I get it -- thanks!

Well, there are many ways you could do it. One simple way is just to code what you told me explicitly; it might look like



Another way to do it would be to have a routine named readCommand() which prompted for a command and didn't return until a valid command had been parsed; then the routine above would always know it was dealing with a valid command. The second one is more modular; it would also allow you to use a switch with an enum instead of the chain of if/thens (because readCommand() could return an enumerated value instead of the actual String command)
Vlad Ivanov
Greenhorn

Joined: Mar 02, 2010
Posts: 7
Great! That worked perfectly! Thank you so much! Such a simple solution... If only all programming languages could be written using a single word. ^_^ Thanks again!
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

You may think of using a while with a switch/case instead of all those ifs/else ifs....



When you do things right, people won't be sure you've done anything at all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Restart a while loop