Возникли проблемы с возвратом и передачей значений; «Подсчет» - PullRequest
0 голосов
/ 06 мая 2018

Нашей задачей было создать игру с угадыванием, в которой компьютер генерировал число, а пользователю предлагалось угадать. Мы должны были создать метод, чтобы играть только в одну игру, а затем создать цикл 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 = ");
   }
}

1 Ответ

0 голосов
/ 06 мая 2018

Посмотрите, когда назначено 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);
   }
...