【问题标题】:Having trouble w/ returning and passing values; "counting"在返回和传递值时遇到问题; “数数”
【发布时间】:2018-10-16 04:35:38
【问题描述】:

我们的任务是创建一个猜谜游戏,计算机会生成一个数字并提示用户猜测。我们要创建一种只玩一个游戏的方法,然后在 main 中创建一个 while 循环以使游戏再次可玩。最后,我们需要显示统计数据。我在展示“最好的游戏”时遇到了麻烦。那是猜测次数最少的游戏。

代码如下:

import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

   public static final int MAX = 100;

   // This is the main. Here we can see a do/while loop
   // and a few variables that were created to compliment it. 
   public static void main(String[] args) {
      Random rand = new Random();
      Scanner console = new Scanner(System.in);
      intro();
      String s = "";
      int totalGames = 0;
      int totalGuess = 0;
      do {
         totalGuess = game(console);
         System.out.print("Do you want to play again? ");
         s = console.next();
         totalGames++;
      } while (s.equals("y") || s.equals("Y") || s.equals("Yes") || 
        s.equals("yes") || s.equals("Yes"));
      totalGuess = totalGuess;
      statistics(totalGames, totalGuess);


   }

   // This method prints out the intro.
   public static void intro() {
      System.out.println("This program allows you to play a guessing 
      game.");
      System.out.println("I will think of a number between 1 and");
      System.out.println(MAX + " and will allow you to guess until");
      System.out.println("you get it.  For each guess, I will tell you");
      System.out.println("whether the right answer is higher or lower");
      System.out.println("than your guess.\n ");
   }

   // This method plays the game only once. It's later used in the main. 
   // Returns the 
   // number of guesses for one game.
   public static int game(Scanner console) {
      Random rand = new Random();
      int random = rand.nextInt(MAX) + 1;
      System.out.println("I'm thinking of a number between 1 and " + MAX + " 
      ... (it's " + random + " )");
      System.out.print("Your guess? > ");
      int guess = console.nextInt();
      int count = 0;
      do {
         if ((random - guess) > 0) {
            System.out.println("It's higher.");
            System.out.print("Your guess? > ");
            guess = console.nextInt();
            count++;
         }
         else if ((random - guess) < 0) {
            System.out.println("It's lower.");
            System.out.print("Your guess? > ");
            guess = console.nextInt();
            count++;
         }
         else if (random == guess) {
            count++;
         }
      } while (random != guess);

      if (count == 1) {
         System.out.println("You got it right on the first guess!!");
      }
      else {
         System.out.println("You got it right in " + count + " guesses.");
      }
      return count;
   }

   // This method prints out the statistics.
   public static void statistics(int x, int y) {
      System.out.println("total games = " + x);
      System.out.println("total guesses = " + (y));
      System.out.println("guesses/game = ");
      System.out.println("best game = ");
   }
}

【问题讨论】:

  • 你遇到了什么麻烦?
  • 由于您在游戏方法的每个 if/else 分支中都调用了count++,因此可以将其拉到循环的顶部。
  • 看起来 Sean 的解决方案(感谢 Sean)解决了“totalGuess”的问题。但是,现在查看代码,我意识到我在游戏中计算猜测的代码也有点不对劲。有什么建议吗?
  • 另外,正如您在底部看到的“统计”方法,有一种称为“最佳游戏”。这样做的目的是打印游戏中猜测次数最少的次数。我不知道该怎么做,所以如果有人可以帮忙。

标签: java parameters return return-value jgrasp


【解决方案1】:

查看totalGuess 的分配情况:

   public static void main(String[] args) {
      Random rand = new Random();
      Scanner console = new Scanner(System.in);
      intro();
      String s = "";
      int totalGames = 0;
      int totalGuess = 0;
      // ^ Initialized to zero
      do {
         totalGuess = game(console);
         // ^ Assigned (not added) to the return value from game.
         // Did you mean: totalGuess += game(console); ?
         System.out.print("Do you want to play again? ");
         s = console.next();
         totalGames++;
      } while (s.equals("y") || s.equals("Y") || s.equals("Yes") || 
        s.equals("yes") || s.equals("Yes"));
      totalGuess = totalGuess;
      // ^ Assigned to itself. No action.
      statistics(totalGames, totalGuess);
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-22
    • 2023-01-16
    • 2020-12-12
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多