Random_num_guesser_game
unknown
java
a month ago
1.4 kB
27
Indexable
// importing our needed classes
import java.util.Scanner;
import java.util.Random;
public class random_num_guesser {
public static void main(String[] args){
// set classes inside variables
Scanner scanner = new Scanner(System.in);
Random random = new Random();
// variables - stored OUTSIDE of loop --> so doesn't change each loop
int random_number = random.nextInt(1, 11);
// asking for input + comparing to random number + looping till "correct"
while (true) {
System.out.println("Guess the random number (1 - 10) > ");
int user_guess = scanner.nextInt();
// if random number is same as users guess
if (random_number == user_guess){
System.out.println("Correct!"); // we print correct (if true)
break; // end the loop / game
} else if (random_number < user_guess){ // random number smaller than user guess?
System.out.println("Too high!"); // users number too high
} else {
System.out.println("Too low!"); // otherwise users number is too low
}
}
System.out.print("Thanks for playing!"); // outside of while loop (runs when loop ends)
}
}Editor is loading...
Leave a Comment