I need help finishing this program. It is two parts, but it is really just one p
ID: 3620942 • Letter: I
Question
I need help finishing this program. It is two parts, but it is really just one program, I just have to turn it to my professor two different times and show the changes I made. The problem is below. Any help is greatly appreciated.---
This assignment has two parts. For the first part, you will be writing a Java program from scratch. The program will allow the user to play a single guess-a-number game. For the second part, you will extend the program you wrote in the first part so that the user can play multiple games.
I am thinking of a number between 1 and 100. Try to guess it.
What's your guess? 50
50 is too small
What's your guess? 75
75 is too big
What's your guess? 62
62 is too small
What's your guess? 69
69 is too big
What's your guess? 65
65 is too big
What's your guess? 63
63 is too small
What's your guess? 64
You've got it in 7 guesses. That was ok!
Goodbye!
The program performs the following actions:
Generate a secret random number between 1 and 100 (see below how to do that), and print a message to communicate that to the user;
Repeatedly ask the user for a guess, and tell the user whether the guess is too big or too small compared to the secret number;
When the user finally guesses the correct number, print a message stating how many guesses were needed, and a message assessing the user's performance according to the following table:
Number of guesses: Message:
1 That was lucky!
2-4 That was amazing!
5-6 That was really good!
7 That was ok!
8-9 That was pretty bad!
10 or more This is not your game!
Generating random numbers
To generate a random number between 1 and 100 and assign the value to a variable x, you can use the following Java statement:
int x = (int)(100 * Math.random ()) + 1;
Math.random is a function that returns a pseudo-random double number greater than or equal to 0.0 and less than 1.0. The (int) notation is called a cast and truncates to an integer the value of the expression following it.
Do you want to play a game? y
I am thinking of a number between 1 and 100. Try to guess it.
What's your guess? 50
50 is too small
What's your guess? 75
75 is too big
What's your guess? 62
62 is too small
What's your guess? 69
69 is too big
What's your guess? 65
65 is too big
What's your guess? 63
63 is too small
What's your guess? 64
You've got it in 7 guesses. That was ok!
Do you want to play another game? y
I am thinking of a number between 1 and 100. Try to guess it.
What's your guess? 50
50 is too big
What's your guess? 25
25 is too small
What's your guess? 37
37 is too small
What's your guess? 43
You've got it in 4 guesses. That was amazing!
Do you want to play another game? n
Goodbye!
The difference between the programs in Part 1 and Part 2 is that now the program repeatedly asks the user if he/she wants to play a game; if the user enters anything but 'y', the program terminates; otherwise the program allows the user to play a game before asking again if the user wants to play another game.
Explanation / Answer
please rate - thanks
part 1
import java.util.*;
public class GuessTheNumber {
public static void main(String [] args) {
int number;
int guess;
int guesses;
int over;
char torf;
Scanner Keyboard=new Scanner(System.in);
int num=(int)(100 * Math.random ()) + 1;;
guesses=0;
over=0;
do {
System.out.println("Guess a number between 1 and 100.");
guess=Keyboard.nextInt();
guesses++;
if (guess<num)
{
System.out.println("Your guess is too low.");
}
else if (guess>num)
{
System.out.println("Your guess is too high.");
}
else
{System.out.println("You got it in "+guesses+" guesses");
if (guesses>=10)
System.out.println("This is not your game");
else if(guesses>=8)
System.out.println("That was pretty bad!");
else if(guesses==7)
System.out.println("That was ok!");
else if(guesses>=5)
System.out.println("That was really good!");
else if(guesses>=2)
System.out.println("That was amazing!");
else
System.out.println("That was lucky!");
over=1;
}
}while(over==0);
}
}
-------------------------------------------
part 2
import java.util.*;
public class GuessTheNumber {
public static void main(String [] args) {
int number;
int guess;
int guesses;
int over;
char torf;
Scanner Keyboard=new Scanner(System.in);
do {
int num=(int)(100 * Math.random ()) + 1;;
guesses=0;
over=0;
do {
System.out.println("Guess a number between 1 and 100.");
guess=Keyboard.nextInt();
guesses++;
if (guess<num)
{
System.out.println("Your guess is too low.");
}
else if (guess>num)
{
System.out.println("Your guess is too high.");
}
else
{System.out.println("You got it in "+guesses+" guesses");
if (guesses>=10)
System.out.println("This is not your game");
else if(guesses>=8)
System.out.println("That was pretty bad!");
else if(guesses==7)
System.out.println("That was ok!");
else if(guesses>=5)
System.out.println("That was really good!");
else if(guesses>=2)
System.out.println("That was amazing!");
else
System.out.println("That was lucky!");
over=1;
}
}while(over==0);
System.out.println("Would you like to play again (y/n)? ");
torf=Keyboard.next().charAt(0);
}while(torf=='y'||torf=='Y');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.