Resolved: Re: Java: Restarting the Main Method Upon User Request

  • From: Jared Wright <wright.jaredm@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Mon, 21 Jan 2008 17:22:26 -0500

Hi all, Thanks to Marlon and everyone else for their feedback. Making the main method trigger the starting of the game and having the game's guts in a separate method seems to be working quite nicely. Apologies for the delay in reporting success, as I was unexpectedly offline for a couple days. Regardless, one more obstacle circumvented and a little more knowledge gained. THanks again.


JW

Marlon Brandão de Sousa wrote:
Hello,
These contains java pseldo code, but will show you what to do. The
idea is to create the main method, which will create a infinite loop
asking the player if them want to play again or not. If so, another
method, containing the game code will be called, otherwise a break
will be thrown so the main method can exit.

Main - method which is started by the java virtual machine:
while(true)
{
char playAgain;
write("play again?");
read(playAgain);
if(playAgain == 'y')
run();
else
break;
}

run: method containing the game itself.

I hope this helps,
Marlon


2008/1/18, Jared Wright <wright.jaredm@xxxxxxxxx>:
Hello all, Here's the 411. I've written a basic guessing game in Java
that asks the user to guess a number between 1 and 100. The point of
interest comes at the end when the user has successfully guessed the
number. I then want to have the user choose to play again or quit,
probably reading in a character variable to get the choice itself. But
if they do want to run the program again, I'm unsure as to how I should
restart the main method from within itself. I'm concerned about
continuous calls to the main method  that don't ever properly finish up.
Is there a better way to do this? Should I use a new, separate method to
query for the restart? If so, I'm still not sure how the first instance
of the main method will be taken care of when the restart method calls
it again. I can post specific code if anyone likes, but this seems like
a somewhat general issue, so I thought it best to separate the quandary
from this specific code if possible. I'm sure such a scenario will come
up again someday. Thanks for any and all feedback.

Best,
Jared
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind





__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts:

  • » Resolved: Re: Java: Restarting the Main Method Upon User Request