如何在多种方法的简单猜测游戏中添加猜测计数和最佳分数?

时间:2013-10-30 05:06:39

标签: jgrasp

因此我无法拥有一个合适的TOTAL猜测计数器。我的代码需要是基本的,所以请不要给我做任何你认为不平凡的事情的建议。目前,我的代码会玩一个游戏,然后询问用户是否想要再玩一次它是一个简单的Y或N.如果是,则播放另一个游戏,如果没有,那么游戏结束并报告每个游戏的结果:总游戏,每场比赛猜测和最佳游戏(报告猜测次数最少的游戏)。我的问题是能够准确计算每场比赛的所有奖项。我能够报告结果并跟踪正确播放的总比赛但我无法弄清楚如何报告跟踪每场比赛猜测的结果,将它们全部加起来,找到最小的比赛。

 import java.util.*;  

public class Guess2{  

   // The range for what numbers the correct answer is allowed to be.
   public static final int MAX = 2;

   // Main method that calls the other methods for the game to run properly.
   public static void main (String [] args) {  
      Scanner console = new Scanner(System.in);  
      introduction(); 
      int userGuess = game(console);
      int numGames = 1;
      System.out.print("Do you want to play again? ");  
      String putYesNo = console.next();  

      while (putYesNo.equalsIgnoreCase("y")) {  
      System.out.println(); 
         game(console);  
         numGames++;
         System.out.print("Do you want to play again? ");  
         putYesNo = console.next();  
      }   
      if (putYesNo.equalsIgnoreCase("n")) { 
         System.out.println();
         finalScores(console,totalGuess,numGames);  
      }  
   }  

   // Method to play the guessing game using the input from the console(parameter) via the user playing the game.
   public static int game(Scanner console){  
      Random answer = new Random(); 
      int correct = answer.nextInt(MAX) + 1;  
      System.out.println("I'm thinking of a number between 1 and " + MAX + "..."); 
      int userGuess = 0;               // This is used to prime the while loop.
      int numGuess = 0;                // This is used to start the count at 1. 
      int totalGuess = 0;    
      while (userGuess != correct){ 
         System.out.print("Your guess? ");  
         userGuess = console.nextInt();  
         if (userGuess > correct){  
            System.out.println("It's lower.");  
         } else if (userGuess < correct){  
            System.out.println("It's higher.");  
         }
         numGuess++;
      } 
      if (userGuess == correct && numGuess == 1){   
         System.out.println("You got it right in 1 guess");  
      } else if (userGuess == correct && numGuess != 1){  
         System.out.println("You got it right in " + numGuess + " guesses");
        }   
        totalGuess =+ numGuess;  
      return totalGuess;                 // This Returns the number of total guesses for this single game.
   }

   /* Method used to report the Users' final statistics of the game(s). 
      Uses information from the user input via the console, 
      the sum of guesses used for every game, and the total number of games played. */
   public static void finalScores(Scanner console, int totalGuesses, int numGames){  
      System.out.println("Overall results:");  
      System.out.println("    total games   = " + numGames);  
      System.out.println("    total guesses = " + totalGuesses);  
      System.out.println("    guesses/game  = " + iLoveRounding(1.0*totalGuesses/numGames));  
      System.out.println("    best game     = " );  
   }  

   // Method that introduces the User to the Game. 
   public static void introduction() {
      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(MAX + " 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();
   } 

   /* Method used to round the "guessing/game" ratio to 1 decimal place.
      The return value of this method returns the ratio in decimal form with only 1 didgit, so
      The scores method can report it properly. */
   public static double iLoveRounding(double round){ 
      return Math.round(round * 10.0) / 10.0;  
   }
}

1 个答案:

答案 0 :(得分:0)

您可以考虑制作一个整数ArrayList来保存每场比赛的所有猜测。然后,当您打印结果时,您可以先打印出整个ArrayList来表示每个游戏的猜测,然后调用'Collections.sort(yourArrayList)',它将ArrayList从最低到最高排序,然后打印出第一个ArrayList的元素。

我建议在一个简单的整数数组上使用整数ArrayList,因为数组需要您事先定义大小。 ArrayList可以是动态的,您可以根据用户玩游戏的次数来更改大小。