Java - Input Challenge Yourself
Create a class called GuessMyNumber. Type or copy/paste the code below.
import java.util.Scanner;
public class GuessMyNumber {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
// Generate a random number from 1 to 10
int myNum = (int) (Math.random() * 10);
int guess = -1;
// Loop as long as guess != myNum
while (guess != myNum) {
// Ask user for guess
System.out.print("Guess a number between 0 and 10: ");
guess = keyboard.nextInt();
// Give a hint to user; use if statements
// TODO: Write 3 if statements to give hints.
}
// Print win message
System.out.println("YOU WIN!");
}
}
ASSIGNMENT:
This program is suppose to generate a random number (1-10) and let the user try and guess it. It should print out a message telling the user if the number they guess was too high, too low or that they guessed it right. The problem is some lazy programmer forgot to add the appropriate IF statements, but they at least told us where to put them (TODO: section).
This program is suppose to generate a random number (1-10) and let the user try and guess it. It should print out a message telling the user if the number they guess was too high, too low or that they guessed it right. The problem is some lazy programmer forgot to add the appropriate IF statements, but they at least told us where to put them (TODO: section).
- Complete program to display appropriate messages/hints as the player guess.
- Change the program to generate numbers 1-100