2013-03-05 78 views
1

我有一個應用程序的數字猜測遊戲,用戶必須猜測一個0到100之間的數字,當他們猜出正確的程序詢問他們是否想在他們完成遊戲時再次玩遊戲時,我會顯示最少數量的猜測遊戲和猜謎遊戲中的最大數量。現在我所得到的是在使用「Math.min(,)」時所有猜測的總和? 如何獲得最小功能?功能代碼進一步在下面。需要幫助最小和最大功能?

  1. leastNumGuesses = Math.min(leastNumGuesses,猜測);

    double rightNum = Math.random() *100; 
    int randomNum = (int) rightNum; //convert the random number to int 
    int tries = 0; //single game gussess output 
    int numberOfGames = 0; 
    int allTries = 0; //accumalates all tries(sum of all tries) 
    int guesses = 0; // guesses of all games combined 
    int gameGuesses = 0; 
    int leastNumGuesses = 100; 
    int mostNumGuesses = 0; 
    
    
        while (choice.equalsIgnoreCase("y")) 
    

    {

    System.out.println(); 
    
        int guess = getIntWithinRange(sc,"Enter the Number: ", 0, 100); 
        tries++; 
        guesses++; 
        gameGuesses++; 
    
        if (guess == randomNum) 
        { 
         numberOfGames++; 
         System.out.println("You got it in " + tries + " tries."); 
         leastNumGuesses = Math.min(leastNumGuesses,gameGuesses); 
         if (tries <=3) 
           System.out.println("Great work! You are a mathematical wizard."); 
         else if (tries > 3 && tries <= 7) 
           System.out.println("Not too bad! You've got some potential."); 
         else if (tries > 7) 
           System.out.println("What took you so long? Maybe you should take some lessons."); 
    
         System.out.println();  
         System.out.println("Would you like to play again (y/n):"); 
         choice = sc.nextLine(); 
    
        while (!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y")) 
        { 
    
        System.out.println("Error! entry must be \"y\" or \"n\"."); 
        System.out.println("Would you like to play again (y/n):"); 
        choice = sc.nextLine(); 
        } 
    
         if (choice.equalsIgnoreCase("y"))  
         { // reset the random number & tries 
    
          rightNum = Math.random() *100; 
          randomNum = (int) rightNum; 
          tries=0; 
          gameGuesses++; 
         } 
         else if (choice.equalsIgnoreCase("n")) 
         {  
          allTries += guesses; 
          int averageNumGuess = allTries/numberOfGames; 
    
          System.out.println("Bye - Come back again"); 
          System.out.println("Number of Games Played: " + numberOfGames); 
          System.out.println("Average Number of Guesses: " + averageNumGuess); 
          System.out.println("Least Amount of Guesses In a Single Game: " + leastNumGuesses); 
    
         } 
    
        } 
    
+2

你能發佈一個最小的代碼片段:1)演示了這個問題,2)是可編譯的? – apnorton 2013-03-05 02:32:13

+0

編譯和運行代碼時會發生什麼?它與你想要和期望的有什麼不同? – 2013-03-05 02:50:36

+0

@Code-Guru它爲每個遊戲添加所有猜測並將其顯示爲單個遊戲中最少的猜測次數;我希望它能夠在每場比賽中進行所有猜測,並在比賽中找到最小或最少的猜測。 – babaysteps 2013-03-05 02:54:37

回答

0

看來你改變你想guesses在節目中代表什麼。

記住guesses數的猜測在所有場比賽,而leastNumGuesses最初設置爲100。在大多數情況下,您會發現guesses < leastNumGuesses,因此Math.min(guesses, leastNumGuesses)函數將返回guesses

修復:使用除guesses之外的變量,例如gameGuesses來跟蹤遊戲中進行了多少次猜測。然後,Math.min(,)將按照您的預期行事。

+0

我試過了但是問題是min函數加上所有的猜測,也許是把變量放在錯誤的地方?我不知道,但這真的讓我感到困擾。 – babaysteps 2013-03-05 02:49:26

+1

@ user1852564在循環內放置'Math.min(guesses,leastNumGuesses)'行,以便每次遊戲結束時執行,無論用戶是否回答「y」或「n」。 – apnorton 2013-03-05 02:57:43