3 Replies - 215 Views - Last Post: 21 January 2019 - 10:46 AM Rate Topic: -----

#1 .ThreadLocalRandom   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 21-January 19

Restart method when inside failed 'if body'

Posted 21 January 2019 - 08:40 AM


Hey, got a quick question that I'm sure is easy for you guys... I'm a newbie, but hey everybody gotta start one day :)

So I'm trying to write a little guessing game, basic stuff.
Now as you read from the code, the player should choose between 1 and 6, the rest is obvious. But now what if he chooses a number that is outside the given range? How can I tell the program to rerun the method in the case that this 'if' statement is true?
As it is right now, a number outside this range just results in the printing of "invalid number" and ends, but that obviously makes the rest obsolete... not ideal, pls help! thanks


public static void main(String[] args) {

        Scanner scanner= new Scanner(System.in);

        System.out.println("Welcome dear player! I guessed of a number between 1 and 6. Try to guess it!");
        System.out.print("Your guess: ");

        int randomNumber = ThreadLocalRandom.current().nextInt(1, 6);
        int playerNumb= scanner.nextInt();

        if(playerNumb<1 || playerNumb>6) {
            System.out.println("That is an invalid number my friend. Try again");
            return;
            }
        else {
            if (playerNumb < randomNumber || playerNumb > randomNumber) {
                System.out.println("Hard luck! Maybe next time.");
            }
            else {
                System.out.println("Correct! Aren't you lucky.");
            }
        }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Restart method when inside failed 'if body'

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,709
  • Joined: 25-December 13

Re: Restart method when inside failed 'if body'

Posted 21 January 2019 - 09:19 AM

To rerun code you will need to put the code inside of a loop like a while loop.
Have a boolean variable that controls when to loop again or to exit the loop.

Work out the logic in pseudo code to get the order of the logic correct before trying to write the code.

This post has been edited by NormR: 21 January 2019 - 09:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 .ThreadLocalRandom   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 21-January 19

Re: Restart method when inside failed 'if body'

Posted 21 January 2019 - 10:06 AM

View PostNormR, on 21 January 2019 - 09:19 AM, said:

To rerun code you will need to put the code inside of a loop like a while loop.
Have a boolean variable that controls when to loop again or to exit the loop.

Work out the logic in pseudo code to get the order of the logic correct before trying to write the code.


Hey, I just figured it out, I repositioned it like this, it works but is there a way to improve it? or is it just fine for its purpose? Great thanks

public static void main(String[] args) {

        Scanner scanner= new Scanner(System.in);

        System.out.println("Welcome dear player! I guessed of a number between 1 and 6. Try to guess it!");
        System.out.print("Your guess: ");

        int randomNumber = ThreadLocalRandom.current().nextInt(1, 6);
        int playerNumb= scanner.nextInt();

        while(playerNumb<1 || playerNumb>6) {
            System.out.print("That is an invalid number my friend. Try again: ");
            playerNumb= scanner.nextInt();
        }
        if (playerNumb < randomNumber || playerNumb > randomNumber) {
            System.out.format("Hard luck! Maybe next time. My number was %d",randomNumber);
            }
        else {
            System.out.println("Correct! Aren't you lucky.");
            }
    }

Was This Post Helpful? 1
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,709
  • Joined: 25-December 13

Re: Restart method when inside failed 'if body'

Posted 21 January 2019 - 10:46 AM

Looks ok to me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1