This site requires JavaScript, please enable it in your browser!
Greenfoot back
basil60
basil60 wrote ...

2014/2/9

How do I stop Greenfoot running my code?

basil60 basil60

2014/2/9

#
Hi I tried making a simple implementation of a guessing game. If you guess high or low, it keeps outputting in the System console, despite my stop method. Input and output appear OK, it's getting it to stop that's the problem.
   public void guessGame(){
       String first_name;
       int ranNum = Greenfoot.getRandomNumber(10) + 1;
       int guess = 0;
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess your number between 1 and 10 here"));
while (guess != ranNum) {                           //while the guess is wrong, do what follows
    
    if (guess < ranNum){
    System.out.println("your guess was too low!");
    Greenfoot.stop();
}
else if (guess > ranNum){
  System.out.println("your guess was too high!"); 
  Greenfoot.stop();
}
else {
   System.out.println("you got it!"); 
    Greenfoot.stop();
}
}
Any advice welcome. BTW, eventually I'd implement this fully in Greenfoot. For now, I just want to run a simple Java implementation that produces a result.
danpost danpost

2014/2/9

#
I think if you add a 'break;' statement after lines 10 14 and 18, that should then do what you want.
basil60 basil60

2014/2/16

#
Tracked my problem down. Doesn't even require breaks or returns. Line 5 above needs to be placed inside the while loop. It was logic error.
danpost danpost

2014/2/16

#
I am glad you figured it out. Also, I apologize. I believe I misunderstood what you were trying to do.
You need to login to post a reply.