我有一個應用程序的數字猜測遊戲,用戶必須猜測一個0到100之間的數字,當他們猜出正確的程序詢問他們是否想在他們完成遊戲時再次玩遊戲時,我會顯示最少數量的猜測遊戲和猜謎遊戲中的最大數量。現在我所得到的是在使用「Math.min(,)」時所有猜測的總和? 如何獲得最小功能?功能代碼進一步在下面。需要幫助最小和最大功能?
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); } }
你能發佈一個最小的代碼片段:1)演示了這個問題,2)是可編譯的? – apnorton 2013-03-05 02:32:13
編譯和運行代碼時會發生什麼?它與你想要和期望的有什麼不同? – 2013-03-05 02:50:36
@Code-Guru它爲每個遊戲添加所有猜測並將其顯示爲單個遊戲中最少的猜測次數;我希望它能夠在每場比賽中進行所有猜測,並在比賽中找到最小或最少的猜測。 – babaysteps 2013-03-05 02:54:37