Java 猜谜游戏中整体最佳游戏的计算

Java 猜谜游戏中整体最佳游戏的计算,java,while-loop,Java,While Loop,所以我为一个数字猜测游戏编写了一个java代码。整个事情差不多完成了。它的工作原理是选择一个随机数,然后询问用户控制台的输入,然后说这是高于还是低于随机数。一旦你猜到了,它就会问你是否想再玩一次。当你最终对此说不时(无论是一场还是几场),它会打印出你的总体结果,包括总游戏数、总猜测数、平均猜测/游戏数和你的最佳游戏数。我已经解决了所有问题,只是我不知道如何让它打印出你的整体最佳游戏 import java.util.*; //so I can use scanner public class

所以我为一个数字猜测游戏编写了一个java代码。整个事情差不多完成了。它的工作原理是选择一个随机数,然后询问用户控制台的输入,然后说这是高于还是低于随机数。一旦你猜到了,它就会问你是否想再玩一次。当你最终对此说不时(无论是一场还是几场),它会打印出你的总体结果,包括总游戏数、总猜测数、平均猜测/游戏数和你的最佳游戏数。我已经解决了所有问题,只是我不知道如何让它打印出你的整体最佳游戏

import java.util.*; //so I can use scanner

public class GuessingGame {
   public static void main(String[] args) {

      Random rand = new Random ();
      int max = 100;
      Scanner input = new Scanner(System.in);
      int guess;
      boolean play = true;
      int totalGames = 0;
      int totalGuesses = 0;

      System.out.println("Can you guess the word?");
      System.out.println("I am sure you cannot guess!");
      System.out.println("Go ahead and try!");
      System.out.println();

      while (play) {

         System.out.println("I'm thinking of a number between 1 and " + max + "...");
         int numberToGuess = rand.nextInt(max) + 1;
         int numberOfTries = 0;
         boolean win = false;
         while (!win) {

            System.out.print("Your guess? ");
            guess = input.nextInt();
            numberOfTries++;

            if (guess == numberToGuess) {
               win = true;  
            } else if (guess > numberToGuess) {
               System.out.println("It's lower.");
            } else if (guess < numberToGuess) {
               System.out.println("It's higher.");
            }      
            input.nextLine();
         }
         if (numberOfTries == 1) {
            System.out.println("You got it right in " + numberOfTries + " guess!");
         } else {
            System.out.println("You got it right in " + numberOfTries + " guesses!");
         }   
            totalGames++;
            totalGuesses+= numberOfTries;
            System.out.print("Do you want to play again? ");

            String answer = input.nextLine();
            char firstLetter = answer.charAt(0);
            if (firstLetter == 'y' || firstLetter == 'Y') {
            play = true;  
            } else {
            play = false;              
         }  
         System.out.println();           
      }
      System.out.println("Overall results:");
      System.out.println("Total games = " + totalGames);  
      System.out.println("Total guesses = " +  totalGuesses);
      System.out.println("Guesses/game = " + totalGuesses/totalGames);
      System.out.println("Best game = ");
   }  
} 
  /* int mintries,bestgame,gamenumber=0;
   bestgamenumber=0;mintreies=Integer.MAX_VALUE:*/
import java.util.*//所以我可以用扫描仪
公开课猜谜游戏{
公共静态void main(字符串[]args){
Random rand=新的Random();
int max=100;
扫描仪输入=新扫描仪(System.in);
智力猜测;
布尔播放=真;
整数totalGames=0;
int totalGuesses=0;
System.out.println(“你能猜出这个词吗?”);
System.out.println(“我相信你猜不到!”);
System.out.println(“继续尝试吧!”);
System.out.println();
边玩{
System.out.println(“我想的是一个介于1和“+max+”之间的数字”);
int numberToGuess=rand.nextInt(max)+1;
int numberofthries=0;
布尔赢=假;
而(!赢){
系统输出打印(“您的猜测?”);
guess=input.nextInt();
numberofthries++;
如果(猜测==数字猜测){
赢=真;
}否则如果(猜测>数字猜测){
System.out.println(“它较低”);
}else if(猜测<数字猜测){
System.out.println(“更高”);
}      
input.nextLine();
}
如果(numberOfTries==1){
System.out.println(“你在“+numberofthries+“guess!”中做对了!”);
}否则{
System.out.println(“你在“+numberofthries+”guesss!”中做对了!”;
}   
totalGames++;
总猜测次数+=尝试次数;
System.out.print(“您想再次播放吗?”);
字符串answer=input.nextLine();
char firstLetter=answer.charAt(0);
if(firstLetter='y'| | firstLetter=='y'){
玩=真;
}否则{
玩=假;
}  
System.out.println();
}
System.out.println(“总体结果:”);
System.out.println(“Total games=“+totalGames”);
System.out.println(“总猜测=”+总猜测);
System.out.println(“猜测/游戏=“+TotalGuess/totalGames”);
System.out.println(“最佳游戏=”);
}  
} 

跟踪最佳分数,即猜测次数最少的分数

int bestGame = Integer.MAX_VALUE; // at the top
bestGame = Math.min(bestGame, numberOfTries); // at the end of your inner while loop

最差的分数是最高的猜测次数,这受到Integer.MAX_值的限制,因此您可以从这里开始。

为了适应您已经编写的代码,您可以

  • 创建一个新的“全局”变量,例如
    int bestGame=Integer.MAX\u值
  • 每当用户完成游戏时,检查当前的
    numberOfGuesses
    是否小于当前的
    bestGame
    ,如果小于,则用当前的
    numberOfGuesses
    覆盖
    bestGame
  • 最后,您只需输出
    bestGame

  • 为了获得最佳游戏,您需要在每场游戏后跟踪最佳游戏,例如检查它的变量,在每场游戏后有一个新的最佳游戏

    最佳游戏是指回答问题所需的最少尝试次数是最佳游戏

    import java.util.*; //so I can use scanner
    
    public class GuessingGame {
       public static void main(String[] args) {
    
          Random rand = new Random ();
          int max = 100;
          Scanner input = new Scanner(System.in);
          int guess;
          boolean play = true;
          int totalGames = 0;
          int totalGuesses = 0;
    
          System.out.println("Can you guess the word?");
          System.out.println("I am sure you cannot guess!");
          System.out.println("Go ahead and try!");
          System.out.println();
    
          while (play) {
    
             System.out.println("I'm thinking of a number between 1 and " + max + "...");
             int numberToGuess = rand.nextInt(max) + 1;
             int numberOfTries = 0;
             boolean win = false;
             while (!win) {
    
                System.out.print("Your guess? ");
                guess = input.nextInt();
                numberOfTries++;
    
                if (guess == numberToGuess) {
                   win = true;  
                } else if (guess > numberToGuess) {
                   System.out.println("It's lower.");
                } else if (guess < numberToGuess) {
                   System.out.println("It's higher.");
                }      
                input.nextLine();
             }
             if (numberOfTries == 1) {
                System.out.println("You got it right in " + numberOfTries + " guess!");
             } else {
                System.out.println("You got it right in " + numberOfTries + " guesses!");
             }   
                totalGames++;
                totalGuesses+= numberOfTries;
                System.out.print("Do you want to play again? ");
    
                String answer = input.nextLine();
                char firstLetter = answer.charAt(0);
                if (firstLetter == 'y' || firstLetter == 'Y') {
                play = true;  
                } else {
                play = false;              
             }  
             System.out.println();           
          }
          System.out.println("Overall results:");
          System.out.println("Total games = " + totalGames);  
          System.out.println("Total guesses = " +  totalGuesses);
          System.out.println("Guesses/game = " + totalGuesses/totalGames);
          System.out.println("Best game = ");
       }  
    } 
    
      /* int mintries,bestgame,gamenumber=0;
       bestgamenumber=0;mintreies=Integer.MAX_VALUE:*/
    
    在您的while(play)上方添加以上行

    在关闭(播放)前添加if条件。 那就好像

             int mintries;
             mintreies=Integer.MAX_VALUE:  
             int gamenumber=0;
              int bestgamenumber=0//if you want to print the which game is the   best game(!st,2nd,3rd..)    ;        
             while(play)
             { 
                 // do all your stuff
                  gamenumber++;
                  if(mintries>numberOfTries)
                  {
                     mintries=numberOfTries;//update mintries
                     bestgamenumber=gamenumber;
                   } 
            }
               }
              System.out.println("Game number +bestgamenumber+"was the best game with"+ mintries+"tries);
    

    我正在考虑您要打印哪个游戏(第一、第二、第三)是最好的,以及猜测最佳游戏的最少尝试次数。如果我错了,请纠正我。

    如果您将其初始化为0,
    numberOfGuesses
    何时可以小于
    bestGame
    :PI获取错误,无法使用
    int bestGame=Integer.Max\u int找到符号这个答案出现在低质量的审查队列中,可能是因为您没有解释代码。如果你解释了(在你的答案中),你就更有可能获得更多的选票,提问者也更有可能学到一些东西!非常感谢你!昨晚这真让我受不了我感谢你的快速反应!事实上不,这比我想做的更复杂。我只是想让它打印出你在最佳游戏类别下的最佳得分。因此,无论哪种游戏的猜测量最低,都将是“最好的游戏”,它将打印出多少猜测。我想出来了,但是谢谢你的帮助,这对以后的参考非常有用。