File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Restart a while loop

 
Vlad Ivanov
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may wish to use "else" and "else if" in your code.
 
Vlad Ivanov
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vlad Ivanov
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1800
12
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may think of using a while with a switch/case instead of all those ifs/else ifs....

 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic