我正在为学校制作这个猜谜游戏。我已经意识到在某些时候我删除了我的while循环,因为用户的猜测等于计算机的随机数,它搞砸了我的程序结果。我以为我可以添加一个嵌套的while循环,但这没有用。我一直试图弄清楚这几个小时。
任何想法如何在我的代码中添加类似while(guess == number)的内容并保持其工作?
/*
Programming Assignment #3: Guess
Peter Harmazinski
Week 8
Guessing Game
*/
import java.util.*;
public class Guess {
public static final int RANGE = 100;
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
boolean again = true;
double guessesDividedByGames = 0;
int maxGuesses = 0;
int numGames = 0;
int numGuesses = 1;
int totalGuesses = 0;
Random rand = new Random();
int number = rand.nextInt(RANGE) + 1;
int guessTracker = 0;
while(again) {
getInstructions();
int guess = getGuess(console);
numGuesses = getHigherLower(guess, number, console);
totalGuesses += numGuesses;
again = playAgain(numGuesses, console);
numGames++;
if (numGuesses > maxGuesses) {
maxGuesses = numGuesses;
}
}
guessesDividedByGames = (double)totalGuesses / numGames;
getResults(numGames, totalGuesses, guessesDividedByGames, maxGuesses);
}
//Prints instructions for user
public static void getInstructions() {
System.out.println("This program allows you to play a guessing game");
System.out.println("I will think of a number between 1 and " + RANGE);
System.out.println("and will allow you to guess until you get it.");
System.out.println("For each guess, I will tell you whether the");
System.out.println("right answer is higher or lower than your guess");
System.out.println("");
}
//Allows the user to play again if first letter of input is "y" or "Y"
public static boolean playAgain(int guessesNum, Scanner console) {
boolean anotherTime = false;
System.out.println("You got it right in " + guessesNum + " guesses.");
System.out.println("");
System.out.print("Do you want to play again? ");
String repeat = console.next();
String[] yesOrNo = repeat.split("");
System.out.println("");
if (yesOrNo[0].equals("y") || yesOrNo[0].equals("Y")) {
anotherTime = true;
}
return anotherTime;
}
//Outputs the results if the user doesn't play again
public static void getResults(int gamesTotal, int guessesTotal, double guessesDividedByGames, int guessesMax) {
System.out.println("Overall results:");
System.out.println("\ttotal games\t= " + gamesTotal);
System.out.println("\ttotal guesses\t= " + guessesTotal);
System.out.println("\tguesses/game\t= " + guessesDividedByGames);
System.out.println("\tmax guesses\t= " + guessesMax);
}
//Tells the user whether the random number is higher or lower
//and then returns the number of guesses
public static int getHigherLower(int guess, int randomNumber, Scanner console) {
int guessIncreaser = 1;
while (guess > randomNumber) {
System.out.println("lower");
guess = getGuess(console);
guessIncreaser++;
}
while (guess < randomNumber) {
System.out.println("higher");
guess = getGuess(console);
guessIncreaser++;
}
return guessIncreaser;
}
//Asks the user to guess the random number
//then returns the guess
public static int getGuess(Scanner console) {
System.out.println("I'm thinking of a number...");
System.out.print("Your Guess? ");
int playerGuess = console.nextInt();
while (playerGuess < 1 || playerGuess > RANGE) {
System.out.println("Out of range, please try again.");
System.out.print("Your Guess? ");
playerGuess = console.nextInt();
}
return playerGuess;
}
}
答案 0 :(得分:1)
你需要的是一个很大的循环而不是两个小的
while (guess != randomNumber) {
if (guess > randomNumber) {
System.out.println("lower");
} else {
System.out.println("higher");
}
guess = getGuess(console);
guessIncreaser++;
}
答案 1 :(得分:1)
问题似乎是你的getHigherLower
方法,特别是这两个方块:
while (guess > randomNumber) {
System.out.println("lower");
guess = getGuess(console);
guessIncreaser++;
}
while (guess < randomNumber) {
System.out.println("higher");
guess = getGuess(console);
guessIncreaser++;
}
如果用户猜到的数字低于randomNumber
,那么更高的两个while
块都会被转义。相反,你想要的是:
while (guess != randomNumber) {
if (guess > randomNumber) {
System.out.println("lower");
}
else {
System.out.println("higher");
}
guess = getGuess(console);
guessIncreaser++;
}
答案 2 :(得分:0)
首先,我很犹豫,只是在代码中给出答案,因为这是针对学校项目的,我们通过挑战自己和实现解决方案来学习。但我愿意指出你正确的方向。
正如其他人所指出的那样,你的两个while循环被设置为导致错误。例如,如果我首先猜测太低,然后太高,你的方法错误地告诉我我猜对了。 这是一个大问题!
- 随机数= 63
- 猜1 = 34 (更低)
- 猜猜2 = 100 (更高)
- 实际上你的程序告诉我我猜测&#34; 100&#34;当数字为&#34; 63&#34;是对的!
// 1st conditional check: 34 !> 63, so skips first while loop
while (guess > randomNumber) {
guess = getGuess(console);
}
// 1st conditional check: 34 < 63, so enters second while loop
// 2nd conditional check: 100 !< 63, so skips second while loop
while (guess < randomNumber) {
// guess now becomes 100, goes back to top of while loop to check condition again
guess = getGuess(console);
}
// returns and exits method here (program wrongly thinks user has guessed correctly!)
请注意,您可以执行
System.out.println("random number: " + number);
测试您是否正确猜测了随机数。您也可以查看一些JUnit testing。
James Ko似乎对better method implementation感觉良好。
使用if语句检查字符串数组中的第一个索引是否等于&#34; y&#34;或&#34; Y&#34;但你的计划永远不会继续。这是为什么?
if (yesOrNo[?].equals("y") {
anotherTime = true;
}
您应该考虑用户输入是否真的放在第一个索引处?
提示:遍历&#34; yesOrNo&#34;数组并打印出每个索引,以查看用户输入在数组中的位置。
for (int i = 0; i < yesOrNo.length; i++) {
System.out.println("String at index " + i + ": " + yesOrNo[i]);
}
祝你好运,并记住测试是你的朋友!