public class GuessingNumber2 {
public static void main(String[] args) {
//game intro
gameIntro();
//random number initializer from 1 - 100
randomNumberInitializer();
//user input guess
Scanner scanner = new Scanner(System.in);
int attempts = 10;
int start = 1;
while (start <= 10) {
int guessNum = scanner.nextInt();
if (guessNum > randomNum) {
System.out.println("Go lower!");
} else if (guessNum < randomNum) {
System.out.println("Go higher!");
} else {
System.out.println("You guessed it!");
}
start += 1;
}
}
public static void gameIntro() {
System.out.println("Welcome to the guessing game!\nGuess a number from 1 to 100!\nYou will have 10 attempts!");
}
public static void randomNumberInitializer() {
Random random = new Random();
int randomNum = random.nextInt(101);
//from 1 - 100
randomNum += 1;
}
}
Hi I am trying to separate code into methods to make it look better and I came across a problem so once I have created a method randomNumberInitializer(), randomNum requires to be initialized so that it is seen by other code but if I initialize it with 0 then the whole game idea of random number creation comes to end, could you please help! I need randomNum to be created randomly by the game. Is there a way?
Aucun commentaire:
Enregistrer un commentaire