Question
I'm thinking of a number... Your guess? 21 higher Your guess? 39 higher Your guess? 60 higher Your guess? 80 higher Your guess? 82 higher
I'm thinking of a number... Your guess? 21 higher Your guess? 39 higher Your guess? 60 higher Your guess? 80 higher Your guess? 82 higher Your guess? 84 higher Your guess? 86 higher Your guess? 88 higher Your guess? 90 higher Your guess? 92 higher Your guess? 94 lower Your guess? 93
You got it right in 12 guesses
Do you want to play again? YES
I'm thinking of a number...
Your guess? 20 higher Your guess? 40 higher Your guess? 60 lower Your guess? 58 lower Your guess? 56
You got it right in 5 guesses
Do you want to play again? No
Overall results: total games = 2
Overall results:
total games = 3
total guesses = 25
I am having trouble entering the code that makes it able to change just the value of the constant to make the program play the game with a range of 1 to any number desired.
I am also missing the code that inserts the result of the max number of guesses used in any single game:
guesses/game = 8.33 (two decimal places. I need a printf command to format 2 decimal places.)
max guesses = 12
So far I have:
import java.util.*;
public class GG {
static int total = 0;
public static int game () {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
System.out.println("Im thinking of a number...");
int win =0;
int count=0;
int a=rand.nextInt(100)+1;
while(win<1) { int x = scan.nextInt();
if(xa) { System.out.println("lower"); System.out.print("Your guess? "); count++; } if(x==a) {
System.out.println("you got it right in "+count+" guesses"); win++; count++; } } return count; }
public static void main (String[] args) {
int c = 0;
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int win =1;
int i=0;
c = game();
total = total + c;
int count = 0;
while(i<1) {
System.out.println("play again?");
String response = scan.next();
if(response.equals("yes")||response.equals("Yes")) {
win++;
c = game();
total = total +c;
}
if(response.equals("No")||(response.equals("no"))) i++; else System.out.println("wrong input"); }
double oink = total/win;
System.out.println("Overall results:");
System.out.println("total games = "+win);
System.out.println("total guesses ="+total);
System.out.println("guesses/game = "+total/win);
} }
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