Java 猜谜游戏有问题

Issue with Java Guessing Game

我正在为学校制作这个猜谜游戏。我已经意识到,在某些时候我删除了我的 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;
   }
}

你需要的是一个大的while循环而不是两个小的

while (guess != randomNumber) {
    if (guess > randomNumber) {
       System.out.println("lower");
    } else {
       System.out.println("higher");
    }
    guess = getGuess(console);
    guessIncreaser++;
}

问题似乎是您的 getHigherLower 方法,特别是这两个 while 块:

  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++;
}

首先,我不太愿意用代码给你答案,因为这是一个学校项目,我们通过 挑战 自己和 实现来学习 解决方案。但我愿意为您指明正确的方向。


1. getHigherLower()

正如其他人所指出的,您的两个 while 循环被设置为会导致错误。例如,如果我先猜得太低,然后又猜得太高,你的方法会错误地告诉我我猜对了。 这是个大问题!

  • Random number = 63
  • Guess 1 = 34 (lower)
  • Guess 2 = 100 (higher)
  • Actually your program tells me my guess of "100" when the number is "63" is correct!
// 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 似乎对 很有感觉。


2。再玩一次()

您使用 if 语句检查字符串数组中的第一个索引是否等于 "y" 或 "Y",但您的程序永远不会继续。这是为什么?

if (yesOrNo[?].equals("y") {
        anotherTime = true;
}

您应该考虑用户输入是否真的放在第一个索引处?

提示:遍历"yesOrNo"数组并打印出每个索引以查看用户输入在数组中的位置。

for (int i = 0; i < yesOrNo.length; i++) {
    System.out.println("String at index " + i + ": " + yesOrNo[i]);
}

祝你好运,记住测试是你的朋友!