Question: I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist.
import java.util.*;
public class GuessingGame {
public static final int maxNUM = 5;
public static void main(String[] args) { Scanner input = new Scanner(System.in); haiku(); int guesses = playGame(input); System.out.print("Do you want to play again? "); String choice = input.next(); char retry = choice.charAt(0); int game = 1; while (retry == 'Y' || retry == 'y') { playGame(input); game++; System.out.print("Do you want to play again? "); choice = input.next(); retry = choice.charAt(0); } if (retry == 'N' || retry == 'n') { reportResults(input, guesses, game); } // int gamesPlayed = repeat(input); // reportResults(input, guesses, game); }
public static int playGame(Scanner input) { Random r = new Random(); int answer = r.nextInt(maxNUM) + 1; // System.out.println("secret number is " + answer); System.out.println("Guess a number between 1 and " + maxNUM + "..."); int response = 0; int guess = 0; while (response != answer) { System.out.print("Your guess? "); response = input.nextInt(); if (response > answer) { System.out.println("It's lower."); } else if (response < answer) { System.out.println("It's higher."); } if (response == answer) { if (guess == 1) { System.out.println("Great Job! You got it right in 1 try"); } else if (response == answer) { System.out.println("You got it right in " + guess + " tries"); } } for (int i = 1; i <= 1; i++) { guess = (guess * 10) % 10; } // System.out.println("guess = " + guess + " -1"); } return guess - 1; }
public static int repeat(Scanner input) { System.out.println(); System.out.print("GAME OVER! Play Again?, (Y,N) "); String choice = input.next(); char retry = choice.charAt(0); int game = 0; while (retry == 'Y' || retry == 'y') { playGame(input); System.out.print("PLAY AGAIN?, (Y/N) "); String nextTry = input.next(); retry = nextTry.charAt(0); game++; } return game + 1; }
// show overall results public static void reportResults(Scanner input, int guesses, int gamesPlayed) { System.out.println("Overall results:"); System.out.println(" total number of games = " + gamesPlayed); System.out.println(" total guesses = " + guesses); System.out.println(" guesses/game = " + 1.0 * guesses / gamesPlayed); System.out.println(" best game = "); }
public static double round1(double round) { return Math.round(round * 10.0) / 10.0;
}
// haiku public static void haiku() { System.out.println("You Guess the number."); System.out.println("Get it wrong and I'll say:"); System.out.println("Higher or lower."); System.out.println(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
