Question
I need to make a number-guessing game in java for the class. using while loop, random class, or other until chapter 5. -------------------------------------------------------------------------------- Output: This
I need to make a number-guessing game in java for the class.
using while loop, random class, or other until chapter 5.
--------------------------------------------------------------------------------
Output:
This program lets you play guessing games.
You will guess the number I'm thinking
The number is between 1 to 100
The number is...
Guess the number: 50
It's too low
Guess the number: 70
It's too high
Guess the number: 60
It's too low
Guess the number: 65
You got it correct in 4 times.
Would you like to play again? y
The number is...
Guess the number: 15
It's too low
Guess the number: 25
It's too high
Guess the number: 20
You got it correct in 3 times
Would you like to play again? n
Overall result:
Total games = 2
Total guess = 7
Guesses/Games = 3.5
Best game = 3
------------------------------------------------------------------
How can I fix my code to get the output as above?
I need to have at least 3 methods as I have already.
1 method for play the game 1 time. 1 method for showing the explanation of the game. 1 method for showing the result.
I tried, but I couldn't make my program have output as above.
----------------------------------------------------------------------------------------
My code:
import java.util.Random;
import java.util.Scanner;
public class Guess{
public static void introduction(){
System.out.println(This program lets you play guessing games.);
System.out.println(You will guess the number Im thinking);
System.out.println(The number is between 1 to 100");
}
public static void Game(){
Random r = new Random();
Scanner s = new Scanner(System.in);
int total = 0;
int guessNumber = 0;
int guess = 0;
int totalGuess = 0;
guessNumber = r.nextInt(100)+1;
System.out.println("I'm thinking of a number between 1 and 100...");
do{
System.out.print("Your guess? ");
guess = s.nextInt();
total++;
if (guess == guessNumber){
System.out.println("You got it right in " + total + " guesses");
totalGuess++;
break;
} else {
if (guess > guessNumber){
System.out.println(Its too high);
totalGuess++;
} else {
System.out.println("It's too low);
totalGuess++;
}
}
} while(guess != guessNumber);
return;
}
public static void getResult(){
Game();
System.out.println("Overall Result");
System.out.println("\t\ttotal games =");
System.out.println("\t\ttotal guesses =");
System.out.println("\t\tguesss/game =");
System.out.println("\t\tbest game =");
}
public static void playAgain(){
Scanner scanner = new Scanner(System.in);
char choose;
while(true){
choose = scanner.next().charAt(0);
if (choose == 'Y' || choose == 'y'){
System.out.println();
Game();
continue;
}else if(choose == 'N' || choose == 'n'){
System.out.println();
getResult();
break;
}else {
continue;
}
}
}
public static void main(String[] args){
introduction();
Game();
System.out.print(Would you like to play again?(y/n)");
while (true){
playAgain();
}
}
}
-------------------------------------------------------------------------------
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