I am trying to create a simple program which creates a random number and asks the user to guess. It will then say higher or lower until the user guesses correct. The problem is, after the user guesses correctly the program will keep looping. I thought that putting the code into a separate method and then calling it in a while loop would work but unfortunately it was no avail. Could anyone suggest how I would correct this?
Below is my code.
package main;
public class NumberGuesser {
public static void main(String[] args) {
code mycode = new code();
while (mycode.guess != mycode.random){
mycode.codebit();
}
}
}
package main;
import java.util.Random;
import java.util.Scanner;
public class code {
Random rand = new Random();
int random = rand.nextInt(1000);
double guess;
public void codebit(){
System.out.println("Guess the number");
Scanner input = new Scanner(System.in);
Double guess = input.nextDouble();
if (guess > random){
System.out.println("Lower");
}
else if (guess < random){
System.out.println("Higher");
}
else if (guess == random){
System.out.println("Well done");
}
}
}