0

I am a student currently using java and I am having a hard time programming a high and low guessing game. I cannot use looping or "while" code. Any thoughts? This is what I have as of now:

public class FinalProject1

{

  public static void main(String [] args)

    {

       System.out.println("Number Guessing Game 1-1000\nGuess a number");
       guess();
    }

  public static int random()

    {
        int x = (int)(1000*Math.random() + 1);
        return x;
    }

  public static void guess()

  {

      int num = random();
      int tries = 0;
      Scanner keyboard = new Scanner(System.in);
      String inputString = keyboard.nextLine();
      int input = Integer.parseInt(inputString);

      if (input > num)

      {
          System.out.println("Guess a higher number");
          inputString = keyboard.nextLine();
      }

      else if (input < num)

      {
          System.out.println("Guess a lower number");
          inputString = keyboard.nextLine();
      }

      else if (num == input)

      {
          System.out.println("You Win");

  }

}

} 
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34

2 Answers2

-1

First, your logic is flawed. If input > num then user guessed too high, and needs to guess a lower number.

Others have suggested to use recursion, and that's the common solution for your problem, and is likely what you need to do, since you likely have just learned about recursion.

But, since I like to be contrary, I'd do it by starting a new thread, like this:

public class FinalProject1 implements Runnable {
    private Scanner keyboard = new Scanner(System.in);
    private int num = random();

    public static void main(String[] args) {
        System.out.println("Number Guessing Game 1-1000\nGuess a number");
        new Thread(new FinalProject1()).start();
    }

    public static int random() {
        return (int) (1000 * Math.random() + 1);
    }

    @Override
    public void run() {
        String inputString = this.keyboard.nextLine();
        int input = Integer.parseInt(inputString);

        if (input > this.num) {
            System.out.println("Guess a lower number");
            new Thread(this).start();
        } else if (input < this.num) {
            System.out.println("Guess a higher number");
            new Thread(this).start();
        } else if (this.num == input) {
            System.out.println("You Win");
        }
    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
-1

A few things..

  1. Your > & < checks for higher and lower are backwards
  2. You need to be using recursion in order to produce your intended behavior without any loops.

    public static void main(String [] args)
    {
    
        System.out.println("Number Guessing Game 1-1000\nGuess a number");
        int num = random();
        Scanner keyboard = new Scanner(System.in);
    
        guess(keyboard, num);
    }
    
    public static int random()
    {
        int x = (int)(1000*Math.random() + 1);
        return x;
    }
    
    public static void guess(Scanner keyboard, int goal)
    {
        String inputString = keyboard.nextLine();
        int input = Integer.parseInt(inputString);
    
        if (input < goal)
        {
            System.out.println("Guess a higher number");
            guess(keyboard, goal);
        }
    
        else if (input > goal)
        {
            System.out.println("Guess a lower number");
            guess(keyboard, goal);
        }
    
        else if (input == goal)
        {
            System.out.println("You Win");
        }
    }
    

Basically what's happening is that we are getting their response and checking if it's > or < the result, telling them, and the immediately calling the guess() method again in order to repeat this process until they get it right.

Wrapping your head around recursion can be fairly difficult in the beginning, just keep practicing

Headline
  • 1,518
  • 1
  • 9
  • 13