I can\'t seem to get my program to work, when I go to compile I get so many erro
ID: 3621379 • Letter: I
Question
I can't seem to get my program to work, when I go to compile I get so many errors!!
I tried fixing the problems but it seems to make it worst!
Someone please Help!
Explanation / Answer
Hope this is clear for you. import java.util.*; public class GuessingGame { // must define the static constant within the class public static final int CONSTANT = 100; public static void main(String[] args) { myHaiku(); // CONSTANT should be declared as a static variable outside main // int CONSTANT = 100; int playedAnswers = 0; int allTries = 0; int bestGame = Integer.MAX_VALUE; String rePlay = ""; Scanner console = new Scanner(System.in); System.out.println("I'm thinking of a number between 1 and " + CONSTANT + "..."); do { int theGuesses = theGame(console); allTries += theGuesses; if (bestGame > theGuesses) { bestGame = theGuesses; } playedAnswers++; System.out.print("Do you want to play again?"); rePlay = console.next(); } while (rePlay.equalsIgnoreCase("y") || rePlay.equalsIgnoreCase("yes")); theStats(playedAnswers, allTries, bestGame); } public static void myHaiku() { System.out.println("Come, lets play a game!"); System.out.println("Its a guessing number game."); System.out.println("Put it to the test!"); System.out.println(); } public static void theStats(int playedAnswers, int allTries, int bestGame) { System.out.println(); System.out.println("Overall results:"); System.out.println("Total games =" + playedAnswers); System.out.println("Total guesses =" + allTries); System.out.println("Guesses/game =" + ((double) allTries / playedAnswers)); System.out.println("Best game =" + bestGame); } public static int theGame(Scanner console) { int theGuesses = 0; Random rand = new Random(); int number; int guess = -1; number = rand.nextInt(CONSTANT); System.out.print("Your guess?"); guess = console.nextInt(); theGuesses++; while (guess != number) { if (guess number) System.out.println("Your guess is too high."); System.out.println("Try again!"); guess = console.nextInt(); theGuesses++; } if (theGuesses == 1) System.out.println("You got it 1 guess :) "); else System.out.println("You got it in " + theGuesses + " guesses"); // int functions must return an int, so we will return the number of // guesses this took return theGuesses; } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.