I am struggling with the second portion of the program. I cant seem to figure ou
ID: 641241 • Letter: I
Question
I am struggling with the second portion of the program. I cant seem to figure out how to calculate the correct number of games, the total guesses, the guess/game and the best game portion of the question.
Link to the question: http://courses.cs.washington.edu/courses/cse142/10sp/homework/5/spec.pdf
Heres what I have so far:
import java.util.*;
public class GuessingGame2 {
public static void main(String [] args) {
Scanner console =new Scanner(System.in);
Random r = new Random();
String playagain;
int
randomnumber,
userguess = 0,
numberofguesses = 1,
totalguesses = 0,
numberofgames = 0,
bestgame = 1000;
do {
randomnumber = r.nextInt(100)+1;
System.out.println("I'm thinking of a number between 1 and 100...");
System.out.println("*** HINT: the answer is: " + randomnumber);
System.out.print("Your Guess? ");
userguess = console.nextInt();
while (userguess != randomnumber) {
if (userguess < randomnumber) {
System.out.println("It's Higher.");
} else if (userguess > randomnumber) {
System.out.println("It's Lower.");
}
System.out.print("Your Guess? ");
userguess = console.nextInt();
numberofguesses++;
}
System.out.println("You guessed it in " + numberofguesses + " guesses!");
numberofgames++;
totalguesses = totalguesses + numberofguesses;
bestgame = numberofguesses;
if(numberofgames < bestgame)
{
bestgame = numberofgames;
}
System.out.print("Play again? ");
playagain = console.next();
System.out.println();
} while (playagain.equalsIgnoreCase("y") || playagain.equalsIgnoreCase("yes"));
double GuessesGame = totalguesses/numberofgames;
System.out.println("Your overall results:");
System.out.println("Total games = " + numberofgames);
System.out.println("Total guesses = " + totalguesses);
System.out.println("Guesses/Game = " + GuessesGame);
System.out.println("Best game: " + bestgame);
}
}
Explanation / Answer
if (userguess < randomnumber) {///change statements in this way
System.out.println("It's Lower.");
}
else if (userguess > randomnumber) {
System.out.println("It's Higher.");
}
numberofgames++;
totalguesses = totalguesses + numberofguesses;
if(noofgames==1){
bestgame = numberofguesses;///execute this stsement only first time
}
if(numberofgames < bestgame)
{
bestgame = numberofgames;
}
and keep stsrtinh do while loop in a functionsuch that if player wants to play secon time you can simply call that function
i think remaining code is fine
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.