Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m thinking of a number... Your guess? 21 higher Your guess? 39 higher Your gu

ID: 3917169 • Letter: I

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 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(x<a)
{
System.out.println("higher");
System.out.print("Your guess? ");
count++;
}
if(x>a)
{
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);

}
}

Explanation / Answer

here i have modified the code as per the requirement..

========================================================

Program:

========================================================

import java.util.*;

import java.text.DecimalFormat;

public class GG
{

static int total = 0;

public static int game ()
{

int max_guesses = 12;

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(count == max_guesses)

{

System.out.println("Sorry max guesses reached.");

break;
}

if(x<a)
{
System.out.println("higher");
System.out.print("Your guess? ");
count++;
}
if(x>a)
{
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();

DecimalFormat fmt = new DecimalFormat("#.00");

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 = "+fmt.format(oink));

}
}


========================================================

Sample Output:

Im thinking of a number...

12
higher
Your guess? 34
higher
Your guess? 50
higher
Your guess? 60
higher
Your guess? 70
higher
Your guess? 80
lower
Your guess? 75
lower
Your guess? 72
lower
Your guess? 71
you got it right in 8 guesses
play again?
yes
Im thinking of a number...
56
lower
Your guess? 78
lower
Your guess? 9
higher
Your guess? 67
lower
Your guess? 56
lower
Your guess? 55
lower
Your guess? 54
lower
Your guess? 32
lower
Your guess? 12
higher
Your guess? 40
lower
Your guess? 30
lower
Your guess? 21
lower
Your guess? 24
Sorry max guesses reached.
wrong input
play again?
yes
Im thinking of a number...
34
lower
Your guess? 20
higher
Your guess? 30
lower
Your guess? 25
lower
Your guess? 23
higher
Your guess? 24
you got it right in 5 guesses
wrong input
play again?
no
Overall results:
total games = 3
total guesses =27
guesses/game = 9.00


========================================================

Kindly Check and Verify Thanks..!!!