Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Query Formulation And Administration Using Oracle And PostgreSQL

Authors: Michael Mannino

8th Edition

1948426951, 978-1948426954

More Books

Students also viewed these Databases questions