Mr. Truncale - Robert E. Lee High School
  • Home
  • Digital Media
    • HTML
  • Video Game Design
  • Comp. Programming I
  • Comp. Programming II
  • AP Computer Science
  • 3D Printing
  • Teacher Resources
  • Contact Me

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).
  1. Complete program to display appropriate messages/hints as the player guess.
  2. Change the program to generate numbers 1-100
TURN IN

Robert E. Lee High School
1809 Market
Baytown, TX 77520
281-420-4535
digital media | computer programming I and II | BIM
ap computer science | architectural cad | video game design
national technical honor society | contact me