Println 打印多次而不是每次循环递增

Println printing multiple times instead of increasing for each loop

我试图让这段代码打印出玩过的游戏数量 (gameNum)。相反,它总是将 gameNum 设置为 2,并打印出最后一次 println 游戏进行的次数。我觉得我在这里犯了一个愚蠢的错误,但我很难找到它。你能给我一个提示而不是答案吗?我想自己解决这个问题。如果没有,请随时继续并写下答案。

谢谢!

import java.util.*;

public class Testing_gameNum {
    public static final int amt = 1;
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        guessCounter(console);
    }

    public static int game(Scanner console) {
        Random rand = new Random();
        int guess = 0;
        int guessNum = 0;
        System.out.printf("I'm thinking of a number...", amt);
        System.out.println();
        int num = 1;

        do {
            System.out.println("Your guess? ");
            guess = console.nextInt();
            guessNum += guessNum;
        } while (guess != num);

        return guessNum;
    }

    public static void guessCounter(Scanner console) {
        int gameNum = 1;
        int guessNum = game(console);
        if (guessNum == 1){
            System.out.printf("You won in %d guesses!", guessNum);
            System.out.println();
        }
        gameNum = gameNum + 1;
        System.out.println("Do you want to play again?");
        String play = console.next();
        if (play.equals("y")) {
            guessCounter(console);
        }
        System.out.println("Number of games: " + gameNum);
    }
}
  • 检查每个使用 gameNum 的地方。我发现它,即在方法 guessCounter(Scanner console) 中 - 并且只在那里。
  • 所以每次调用此方法时,gameNum 的值都会初始化为 1。游戏获胜后,将其递增 1,然后打印它,因此输出中为 2。
  • int gameNum = 1; 移出方法 guessCounter(Scanner console)。这应该有所帮助。

除此之外,请查看代码块

if (play.equals("y")) {
  guessCounter(console);
}

想象一个玩家不停地玩,总是选择 "y"。在每一轮游戏中,您都会创建另一个级别的递归。这个"do you want to play again"可以通过do-while循环来实现,这样可以避免递归。

尝试这样的事情:

public class Testing_gameNum
{
   public static final int amt = 1;
   public static void main(String[] args)
   {
      Scanner console = new Scanner(System.in);
      guessCounter(console);
   }

   public static int game(Scanner console)
   {
      Random rand = new Random();
      int guess = 0;
      int guessNum = 0;
      System.out.printf("I'm thinking of a number...", amt);
      System.out.println();
      int num = 1;
      do
      {
         System.out.println("Your guess? ");
         guess = console.nextInt();
         guessNum += amt;
      }
      while (guess != num);
      return guessNum;
   }
   public static void guessCounter(Scanner console)
   {
      int gameNum = 1;
      do
      {
         int guessNum = game(console);
         if (guessNum == 1)
         {
            System.out.printf("You won in %d guesses!", guessNum);
            System.out.println();
         }
         gameNum = gameNum + 1;
         System.out.println("Do you want to play again?");
         String play = console.next();
      }
      while (play.equals("y"))
      System.out.println("Number of games: " + gameNum);
   }
}

您已将 guessCounter 定义为递归方法,但出于多种原因,这可能不是您想要的。首先,每次调用 guessCounter 时,您都会创建一个新的 gameNum 并将其设置为 1。您玩游戏并将其递增到 2,但随后递归并且再也不会触及该变量,这就是您的错误的原因。此外,(尽管这在通常的游戏中不太可能发生),如果您玩游戏的次数足够多,您的筹码可能会溢出。每次玩游戏时,计算机都需要记住它需要 return 的代码点,直到它完成 guessCounter 的调用。最终你会 运行 内存不足来存储这些指针。递归对某些问题有好处,但大多数时候最好使用循环。

使用循环而不是递归如何?

我的问题解决了!这是代码(下面的解释):

public class Testing_gameNum {
    public static final int amt = 1;
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        guessCounter(console);
    }

    public static int game(Scanner console) {
        Random rand = new Random();
        int guess = 0;
        int guessNum = 0;
        System.out.printf("I'm thinking of a number...", amt);
        System.out.println();
        int num = 1;

        do {
            System.out.println("Your guess? ");
            guess = console.nextInt();
            guessNum ++;
        } while (guess != num);

        return guessNum;
    }

    public static void guessCounter(Scanner console) {
        int gameNum = 0;
        String play = "y";
            do {
                int guessNum = game(console);
                gameNum += 1;
                if (guessNum == 1) {
                    System.out.printf("You won in %d guesses!", guessNum);
                    System.out.println();
                }
                System.out.println("Do you want to play again?");
                play = console.next();
            } while (play.equals("y"));
        System.out.println("Number of games: " + gameNum);
    }
}

我的问题:gameNum 每次调用 guessCounter 时都在重置。我需要一个 do/while 循环;这样,我可以在方法内部初始化 gameNum,然后只循环需要重复的方法部分。重复的 println 与同一个问题有关:每次我调用 guessCounter 时都会重新打印,而不是我想重复的那部分代码。

感谢大家的帮助!