drew bry
drew bry

Reputation: 1

Java Basic High:Low Guessing Game Help (Loops)

I stopped programming for a while now. Probably around 4 years, and I was just looking to mess around with it, so I decided to make a high:low number guessing game. (guess a number 1-100, program says if your guess is too high or too low) and I completely forgot how I would go about:

a) Once the user guesses the correct number, asking if they want to play again b) If they don't guess the correct number (too high or too low), the program lets them guess again.

I understand that you would need loops, but I just forgot about how I would go about them

package highlow;

import java.util.Random;
import java.util.Scanner;

public class guessing {
    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        Random rand = new Random();
        int tries;
        int correctNum = rand.nextInt(100);

        System.out.println("enter a number 1-100");
        int guess1 = input.nextInt();

        if(guess1 < correctNum){
            System.out.println("number is too low!");
        }
        else if(guess1 > correctNum){
            System.out.println("Number is too high!");
        }
        else if(guess1 == correctNum){
            System.out.println("correct!");
        }
        else{
            System.out.println("not a valid option");
        }


    }

}

Upvotes: 0

Views: 17938

Answers (2)

Kabir Peshawaria
Kabir Peshawaria

Reputation: 19

package highlow;

import java.util.*;

public class guessing
{
    public static void main (String [] args)
    {
        boolean wantstoplay = true;

        while(wantstoplay)
        {
            play();
            System.out.println("Would you like to play again?");
            Scanner kb = new Scanner (System.In);
            if ((kb.nextLine().equals("yes") || (kb.nextLine().equals("Yes"))
                wantstoplay = true;
            else
                wantstoplay = false;
        }

    }
    public void play()
    {
        boolean playing = true;
        int correctNum = (int) ((Math.Random() *100) + 1);
        //selects random double from [1,101) and then rounds down
        int tries = 0;

        while (playing)
        {
            Scanner input = new Scanner(System.in);

            System.out.println("enter a number 1-100");
            int guess = input.nextInt();

            if(guess < correctNum){
                System.out.println("number is too low!");
                tries++;
            }
            else if(guess > correctNum){
                System.out.println("Number is too high!");
                tries++;
            }
            else if(guess == correctNum){
                System.out.println("correct!");
                if (tries > 1)
                    System.out.println("Congrats, you guessed the right number. It only took you " + tries + " attempts!");
                else
                    System.out.println("You guessed it first try! good job");
            }
            else{
                System.out.println("not a valid option");
            }

       }

    }
}

Above is some sample code that might be helpful. I suggest making a play method, and then calling it in your main method. This makes your code more organized and readable, because now you'll get the functionalities you desired without having 1 messy method with 2 loops in it.

You'll notice I included while loops rather than for loops. This is because while loops are ideal when you don't know how many times you're going to need to iterate.

The while in the main method checks to see whether the user would like another game. Notice that it assumes that the user wants to play at least one game. I did this by setting wantstoplay as true before we entered the loop, but this also could've been done with a do-while loop. For more, see (http://www.java-examples.com/do-while-loop)

The while in the play method checks to see whether the user needs to make another guess because he hasn't gotten the answer yet. Just like we can't know how many times the user wants to play before hand, we can't know how many guesses the user will take either.

Hope this helps you get back into programming!

Upvotes: 0

Zach
Zach

Reputation: 4792

You need to wrap everything in a while loop so that it keeps repeating until the user guesses correctly:

// Make the scanner, get the random number etc... Put all the setup and
// stuff you don't want to be repeated here

while (true) {
    System.out.println("enter a number 0-99"); // Changed from 1-100 because rand.nextInt(100)
                                               // returns a number between 0 and 99
                                               // You can do correctNum += 1 to make it between 1 and 100
                                               // But put this in before the while loop starts
    int guess1 = input.nextInt();

    if(guess1 < correctNum){
        System.out.println("number is too low!");
    }
    else if(guess1 > correctNum){
        System.out.println("Number is too high!");
    }
    else if(guess1 == correctNum){
        System.out.println("correct!");
        break; // <---- Add this, this will make the loop stop when the 
               //player gets the answer correct and therefore the program will end
    }
    else{
        System.out.println("not a valid option");
    }
}

While loops repeat whatever is inside them until the statement inside their () is false. In our case the loop will go forever because true is inside the () but with the break statement, the loop will end when the user guesses correctly.

Upvotes: 4

Related Questions