The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 313 Weeks Ago, 6 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Wanted to make print best guesses with the least amount of guesses but can't seem to get my code to work please help.
Â
Â
import java.util.*;
Â
public class Guess {
 Â
  public static int userGuesses;
  public static int userGames;
 Â
  public static void main(String[] args) {
   Intro();
   Scanner prompt = new Scanner(System.in);
   game();
  }
 Â
  //Introduction to the User
 Â
  public static void Intro() {
   System.out.println("This program allows you to play a guessing game.");
   System.out.println("I will think of a number between 1 and");
   System.out.println("100 and will allow you to guess until");
   System.out.println("you get it. For each guess, I will tell you");
   System.out.println("whether the right answer is higher or lower");
   System.out.println("than your guess.");
   System.out.println();
  }
 Â
  //Finds random number and prompts user to guess the number, then giving hints
 Â
  public static double game() {
   userGames++;
   Random goal = new Random();
   Scanner prompt = new Scanner(System.in);
   System.out.println("I'm thinking of a number between 1 and 100...");
   int output = goal.nextInt(5);
   int userInput = -1;
   while (userInput != output) {
     System.out.print("Your guess? ");
     userInput = prompt.nextInt();
     userGuesses++;
     if (userInput < output) {
      System.out.println("It's higher.");
     }
     else if (userInput > output) {
      System.out.println("It's lower.");
     }
     else {
      System.out.println("You got it in " + userGuesses + " guesses");
     }
   }
   again(prompt);
   return userInput;
   Â
  }
 Â
  //Reports the results to the console if user decides to stop playing
 Â
  public static double results() {
   int userInput = 0;
   System.out.println();
   System.out.println("Overall results:");
   System.out.println("  total games = " + userGames);
   System.out.println("  total guesses = " + userGuesses);
   System.out.println("  guesses/game = " + userGuesses/userGames);
Â
Â
Â
Â
Â
Â
Â
Â
Â
   System.out.println("  best game  = " );
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
   return userInput;
  }
 Â
  //Asks user if he would like to play again
 Â
  public static double again(Scanner prompt) {
   String input = null;
   int userInput = 0;
   Scanner console = new Scanner(System.in);
   System.out.print("Do you want to play again? ");
   input = console.nextLine();
   if (input.equals("Y")) {
     return game();
   }
   else if (input.equals("y")) {
     return game();
   }
   else if (input.equals("N")) {
     return results();
   }
   else if (input.equals("n")) {
     return results();
   }
   return userInput;
   Â
  }
}
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly