0

所以我无法拥有正确的 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;  
   }
}
4

1 回答 1

0

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

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

于 2013-10-30T05:22:42.753 回答