我有一个应用程序的数字猜测游戏,用户必须猜测一个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