So I am trying to use a while loop to continue asking for input while the user's input of a random number does not equal the random number generator's output. However when I input the number the higher/lower outputs do not work. It either always says its higher or always says its lower, regardless of the actual numerical value. Help?
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static final int MAX = 100;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int rand1 = rand.nextInt(MAX);
introduction();
game(scan, rand1);
}
public static void introduction() {
System.out.println("This program allows you to play a guessing game."
" I will think of a number between 1 and" +
" 100 and will allow you to guess until" +
" you get it. For each guess, I will tell you" +
" whether the right answer is higher or lower" +
" than your guess");
}
public static void game(Scanner scan, int rand1) {
System.out.println("I'm thinking of a number between 1 and 100...");
System.out.println(rand1);
System.out.println("Your guess?");
int input = scan.nextInt();
while(input != rand1) {
if (input < rand1) {
System.out.println("It's higher");
scan.nextInt(input);
}
else if (input > rand1) {
System.out.println("It's lower");
scan.nextInt(input);
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire