打印多次打印而不是增加每个循环

时间:2017-10-29 20:28:12

标签: java count println accumulate

我正在尝试将此代码打印出来的游戏数量(gameNum)。相反,它始终将gameNum设置为2,并打印出最后一次打印游戏的次数。我觉得我在这里犯了一个愚蠢的错误,但我找不到它。能不能给我一个提示而不是答案?我想自己解决这个问题。如果没有,那么请随意写下答案。

谢谢!

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

4 个答案:

答案 0 :(得分:0)

  • 检查是否使用了gameNum个地点。我在方法guessCounter(Scanner console)中找到它 - 并且仅在那里。
  • 因此,每次调用此方法时,gameNum的值都会初始化为1.赢得游戏后,将其递增1,然后再打印它,因此输出中为2。
  • int gameNum = 1;移出方法guessCounter(Scanner console)。这应该会有所帮助。

除此之外,请查看代码块

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

想象一下玩家一直在继续,总是选择“y”。每一轮游戏都会创建另一个级别的递归。这个“你想再玩一次”可以通过do-while循环实现,这样可以避免递归。

答案 1 :(得分:0)

尝试这样的事情:

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

答案 2 :(得分:0)

您已将guessCounter定义为递归方法,但由于多种原因,这可能不是您想要的。首先,每次拨打guessCounter时,您都需要创建新的gameNum并将其设置为1。你玩游戏并将其增加到2,但随后递归并再也不会再触摸该变量,这是导致你的错误的原因。另外,(虽然这在通常的比赛中不太可能发生),如果你玩足够多次游戏,你可能会溢出你的筹码。每次玩游戏时,计算机都需要记住完成guessCounter调用时需要返回的代码点。最终你将耗尽内存来存储这些指针。递归对某些问题有好处,但在大多数情况下使用循环会更好。

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

答案 3 :(得分:0)

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

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时都会重新打印,而不是我想重复的部分代码。

感谢大家的帮助!