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: | 337 Weeks Ago, 2 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