Write a program where a random number is generated. Then the user tries to guess the number. If they guess too high display something to let them know, and same for if they guess a number that is too low. The loop must iterate until the number is guessed correctly.

import java.util.Random;
import java.util.Scanner;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // stores actual and guess number
        int answer, guess;
  
          // maximum value is 100
        final int MAX = 100;
  
        // takes input using scanner
        Scanner in = new Scanner(System.in);
  
        // Random instance
        Random rand = new Random();
  
        boolean correct = false;
  
        // correct answer
        answer = rand.nextInt(MAX) + 1;
  
        // loop until the guess is correct
        while (!correct) {
  
            System.out.println(
                "Guess a number between 1 and 100: ");
  
            // guess value
            guess = in.nextInt();
  
            // if guess is greater than actual
            if (guess > answer) {
                System.out.println("Too high, try again");
            }
  
            // if guess is less than actual
            else if (guess < answer) {
                System.out.println("Too low, try again");
            }
  
            // guess is equal to actual value
            else {
  
                System.out.println(
                    "Yes, you guessed the number.");
  
                correct = true;
            }
        }
        System.exit(0);
    }
}

Different Example (currently not working)

import java.util.Scanner

Scanner sc = new Scanner(System.in); 
System.out.println(sc.nextInt());

Random n = newRandom();
int newNumber;
int oldNumber = -1; //Will never be matching the random number.
for(int i=0;i<100;i++){
    Scanner sc = new Scanner(System.in); 
    System.out.println(sc.nextInt());

    do{
        newNumber = n.nextInt(100);
    } while (newNumber == oldNumber);
    System.out.println(newNumber);
    oldNumber = newNumber; 
}
82
19
30
78
54
43
66
74
13
46
40
39
2
3
51
19
6
19
26
28
31
44
13
48
47
91
81
69
80
2
77
14
16
95
51
41
87
80
13
3
24
13
3
57
94
79
16
56
69
99
67
65
53
20
9
67
26
82
34
15
39
74
60
94
81
94
19
18
67
55
13
87
94
97
4
17
44
42
6
44
94
37
84
92
10
98
4
35
71
94
44
71
36
45
84
25
53
33
19
4