0
votes

Write a program to generate a random number between 1 – 100 and keep it as a secret number. The program will then check if a user can guess the secret number. The user can continue guessing the number until the number is found or the user can enter 0, which will terminate the program.

Each time the user makes a guess, the program will report as below:

  1. Way Too High or Way Too Low (more than 30 off)
  2. High or Low (between 10 and 30 points off)
  3. A Little High or A Little Low (less than 10 points off)

If secret number is 74 and the user enters 26, the program will print "Way too Low". If the user then says 65, then the program will print "A Little Low

I'm getting stuck on the if statements, maybe my structure isn't correct. I'm not sure .

import java.util.Scanner;
public class SecretNumber {
    public static void main(String[] args){
        int random1, answer;
        Scanner input = new Scanner(System.in);

        random1 = (int)(Math.random()*10);
        System.out.print(random1);

        System.out.println("Guess the number");
        answer = input.nextInt();

        while(answer != 0) {

            if (answer > (random1 + 30)){

                System.out.println("Way to high");
            }
            else if ( answer > ( random1 - 30)){

                System.out.println("Way to low");

            }
            else if (answer > random1 + 10 && answer < random1 + 30){

                System.out.println("High");

            }
            else if (answer > random1 - 10 && answer < random1 - 30 ){

                System.out.println("Low");

            }
            else if ( answer > random1 + 10){

                System.out.println("A little high");
            }
            else if ( answer < random1 - 10){

                System.out.println("A little low");

            }
            else if ( answer == random1){

                System.out.println("That is correct");
                System.exit(0);
            }
            else {
                System.out.println("Guess the number");
                answer = input.nextInt();
            }
        }
    }
}
2
What problems are you having? How is it not turning out how you expected? - Reinstate Monica -- notmaynard
Note that random1 = (int)(Math.random()*10); will give you 0 <= random1 < 10 - Reinstate Monica -- notmaynard
Well it gets stuck on the very first if statement either saying way to high or way to low in a never ending loop - Craig Rabara

2 Answers

2
votes

Move the lines where you get input from the final else block to past the else (but still within your loop) and it will work properly.

The reason you want to do this is because it enters one of the guess again conditions (e.g., "Way too low!") and then never enters the final else, thus resulting in an infinite loop.

    while (answer !=0){

        // ommitted

        else if ( answer == random1){

            System.out.println("That is correct");
            System.exit(0);
        }

        System.out.println("Guess the number");
        answer = input.nextInt();
    }
0
votes

I changed a bit and formatted in a way That I feel is better however you can copy and paste and reformat it to your liking, I also removed the way off and just changed it to lower than or higher than but you can add that back if you like

/*
*/
package randomnumbergame;

import java.util.Scanner;

public class Randomnumbergame 
{

public static void main(String[] args) 
{
    int random1, answer;

    Scanner input = new Scanner(System.in);

    random1 = (int)(Math.random()*100);


    System.out.println("Guess the number I am thinking of, it is between 1 and a 100 or press 0 to quite");
    answer = input.nextInt();

    while(answer != 0) 
    {

        if (answer > random1)
        {

            System.out.println("Your guess is to high");
        }
        else if ( answer > random1)
        {

            System.out.println("Your guess is to low");

        }         
        else 
        {

            System.out.println("That is correct");

        }

    System.out.println("Guess the number I am thinking of, it is between 1 and a 100 or press 0 to quite");
    answer = input.nextInt();

    }
} 
}

On a different note the program is working for me but no matter what I seem to guess It usually seems to tell me I am correct =/ , and I know the math.random part is correct because that Is what I used in class today and that is also what my instructor told me to use.