Question
I am trying to put together my final results for my code. It executes something like.. I'm thinking of a number... Your guess? 20 higher
I am trying to put together my final results for my code. It executes something like..
I'm thinking of a number... Your guess? 20 higher Your guess? 40 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
I have a peice but not the complete thing. I am missing the code that inserts the following:
Overall results:
total games = 3
total guesses = 25
guesses/game = 8.33 (two decimal places. I believe I need a printf command to format 2 decimal places.)
max guesses = 12
So far I have:
import java.util.*; public class GuessingGame { 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) { Scanner scan = new Scanner(System.in); Random rand = new Random(); int win =1; int i=0; game(); int count = 0; while(i<1) { System.out.println("play again?"); String response = scan.next(); if(response.equals("yes")||response.equals("Yes")) { win++; game(); } if(response.equals("No")||(response.equals("no"))) i++; else System.out.println("invalid input"); } double oink = count/win; System.out.println("Overall results:"); System.out.println("total games = "+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