Re: Java: Restarting the Main Method Upon User Request

  • From: "Marlon Brandão de Sousa" <splyt.lists@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Fri, 18 Jan 2008 11:02:58 -0200

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
>
>


-- 
When you say "I wrote a program that crashed Windows," people just
stare at you blankly and say "Hey, I got those with the system, for
free."
Linus Torvalds
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: