Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trouble with loops and invoking a method

 
Christine Marie
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for the help with corrections on the basic version of this lab.

What I have is a basic math game - it asks for your name and choice of operation, spits out random numbers, and compares your answer to the actual answer and gives a result. Then, it ends. Now, my teacher wants us to add an option for the player to continue going until they type some option for "quit".

By itself, I was able to add a System.exit that ran properly and ended the game. However, the best way I could figure to continue was to offer another choice point- "would you like to continue? press 6. If you would like to quit, press 7". I tried writing a loop that either invokes the run method or quits, but I keep getting an illegal start of expression error. (Way way at the bottom.)

Am I barking up the wrong tree with trying to invoke this method again? Suggestions?

All help is appreciated!


 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just took a quick glance, but that semicolon in line 213 looks troublesome.
 
Christine Marie
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed, thanks =) It got stuck there when I was jockeying some things about... but I took it out, and did a test print on both of those loops. They both run, so I know my loop is valid, I'm just at a loss as to how to create this infinite loop scenario.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the Java api for the Integer Class; specifically looking at the parseInt method. Look at what the method does, does it convert an integer to a string? or a string to an integer? then take a look at your program and the if then conditions that evaluate your choice variable.

Java Integer Class:
http://java.sun.com/javase/6/docs/api/java/lang/Integer.html

As far as the infinite loop goes you could use a boolean value and a while loop;

Hope this helps,
Hunter
 
Manjunatha Kampli Kottal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ideal way of coding a menu driven program is to use a infinite loop either with while loop or with for loop until the execution stops along with a switch control. for instance
for(;;)
{

InputReader choice= new InputReader();
int operation= Integer.parseInt(choice);
switch(operation)
{
case 1:System.out.println("Do something here");
break;
case 2:System.out.println("Do something here");
break;
.......................
........................
........................
case 6:System.out.println("Bye Bye");
System.exit(0);
default:System.out.println("Invalid Choice.Please enter the correct Choice.");

}

}

Its not rule but this is how its practiced by the programmers as it simplifies your debugging process
 
salvin francis
Bartender
Posts: 1280
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manju Arasula, welcome to Coderanch.
use code tags next time to post any code.


My personal choice in such situation would be a do-while loop since you want the loop to run at least once.
@ the next of the loop, the user is prompted to continue, if he chooses yes, the loop continues again...


but thats just me.(I am weird at times )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic