Answered step by step
Verified Expert Solution
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). 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). 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");
- (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);
// 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");
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.
- (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!"); }
- (5). Run the project to ensure it works as expected.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started