Question: (1). In Eclipse, create a new Java project named NumberGuess . Review Activity 4 for details. Name the package numberguess . When creating the class,

  1. (1). In Eclipse, create a new Java project named NumberGuess. Review Activity 4 for details. Name the package numberguess. When creating the class, check the box to create a main() method.
  2. (2). In the main() method, add the following code, which will define some variables, generate a random number, and display a message for the user:
// Define program variables int randNum, guessNum, attemptNum; // Generate a random number between 1 and 10 randNum = new java.util.Random().nextInt(10) + 1; // Display a message System.out.println ("I am thinking of a random number between 1 and 10");
  1. (3). Using a for loop, ask for three guesses, using the attemptNum variable. See Chapter 1 in your textbook for details on for loops. You can use the following code to ask for a guess:
System.out.print("Guess what it is?"); java.util.Scanner scan = new java.util.Scanner(System.in); guessNum = scan.nextInt(); System.out.println("You guessed " + guessNum);

Note: The input/output code will be explained in a later lesson. Dont worry about understanding all of it now.

Using an if statement in the for block, determine whether randNum and guessNum are equal. See Chapter 1 in your textbook for more details. You can use the following code if randNum and guessNum are equal:

System.out.println("You guessed it!"); break;

If theyre not equal, you should display:

System.out.println("Sorry, try again!");

Note: The break statement will be explained in the next lesson.

  1. (4). When youre finished, the contents of the main() method should resemble the following:
// Define program variables int randNum, guessNum, attemptNum; // Generate a random number between 1 and 10 randNum = new java.util.Random().nextInt(10) + 1; // Display a message System.out.println ("I am thinking of a random number between 1 and 10"); // Ask for a guess and check the input for ( put your code here ) { System.out.print("Guess what it is?"); java.util.Scanner scan = new java.util.Scanner(System.in); guessNum = scan.nextInt(); System.out.println("You guessed " + guessNum); if ( put your code here ) { System.out.println("You guessed it!"); break; } System.out.println("Sorry, try again!"); }
  1. (5). Run the project to ensure it works as expected.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!