fork download
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class GuessingGameBest
  5. {
  6. public static final int MAX = 5;
  7. public static void main(String[] args)
  8. {
  9.  
  10. //Create a scanner object to read from the keyboard
  11. Scanner keyboard = new Scanner (System.in);
  12.  
  13. //Create a random object
  14. Random number = new Random();
  15.  
  16. //Identifier declarations
  17. int num;
  18. int guesses;
  19. int guess=MAX;
  20. int bestGuess=MAX; //variable for keeping track of best guess
  21. int totalGames=0;
  22. int totalGuesses=0;
  23.  
  24. //Begins program
  25. String playAgain;
  26. haikuIntroduction();
  27. do {
  28. num = number.nextInt(MAX) + 1;
  29. programIntroduction();
  30. for (guesses = 1 ; guesses <= MAX ; guesses++) {
  31. System.out.print("Your guess? ");
  32. guess = keyboard.nextInt();
  33. totalGuesses++;
  34. if (guess > num) {
  35. System.out.println("It's lower.");
  36. }
  37. else if (guess < num) {
  38. System.out.println("It's higher.");
  39. }
  40. else if (guess == num) {
  41. System.out.println("You got it right in " + guesses + " guesses!");
  42. break;
  43. }
  44. }
  45. if(bestGuess>guesses) //for updating the best guess variable
  46. bestGuess=guesses;
  47. totalGames++;
  48. System.out.print("Do you want to play again? ");
  49. playAgain = keyboard.next();
  50. }
  51. while (playAgain.startsWith("y") || playAgain.startsWith("Y"));
  52. System.out.println();
  53. System.out.println("Overall Results:");
  54. System.out.println("Total games = " + totalGames);
  55. System.out.println("Total guesses = " + totalGuesses);
  56. double grade = (double)totalGuesses / (double)totalGames;
  57. System.out.println("Guesses/game = " + grade);
  58. System.out.println("Best game = " + bestGuess);
  59. }
  60.  
  61. //Haiku introduction
  62. public static void haikuIntroduction() {
  63. System.out.println("Let's Play a Game Now.");
  64. System.out.println("It is a Fun Guessing Game.");
  65. System.out.println("Try Your Best, Begin.");
  66. }
  67.  
  68. //Program introduction
  69. public static void programIntroduction() {
  70. System.out.println();
  71. System.out.println("I'm thinking of a number between 1 and 5...");
  72. }
  73. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:4: error: class GuessingGameBest is public, should be declared in a file named GuessingGameBest.java
public class GuessingGameBest
       ^
1 error
stdout
Standard output is empty