java—如何在函数中保留计数值?

xn1cxnb4  于 2021-07-08  发布在  Java
关注(0)|答案(3)|浏览(272)

我需要做一个程序来模拟一个垃圾游戏。但我有问题的是要计算玩家赢或输的次数。我认为这是因为由于count不在main方法中,它将自己设置回0,但我不知道如何修复它。下面是运行程序的代码

public class Craps {

  public static void main(String args[]) {

    JOptionPane.showMessageDialog(null, "Welcome to my Craps game");
    JOptionPane.showMessageDialog(null, "Press 'OK' to roll the dice");

    //Calls the craps function
    playCraps();

    //Asks user if they want to play again
    String playAgain = JOptionPane.showInputDialog("Would you like to play again?\n(enter 'y' for yes)");

    while(playAgain.equals("y")){
      playCraps();
    }

  }

  static void playCraps(){
    Die die1 = new Die(6);
    Die die2 = new Die(6);

    boolean firstTry = false;

    //Generates the die
    int roll1 = die1.roll();
    int roll2 = die2.roll();

    int sum = roll1 + roll2;

    //counts the amount of times the player wins or loses
    int winCount = 0;
    int loseCount = 0;

    //Shows the rolls and the sum
    JOptionPane.showMessageDialog(null, "You rolled a " + roll1 + " and a " + roll2 + ".\n\nThe sum is " + sum);

    //If the player doesn't win or lose on their first try,
    //it keeps rolling until they win or lose
    if(sum == 7 || sum == 11){
      JOptionPane.showMessageDialog(null, "You win!!!");
      winCount++;
      firstTry = true;
    }else if(sum == 2 || sum == 3 || sum == 12) {
      JOptionPane.showMessageDialog(null, "You lose.");
      loseCount = loseCount + 1;
      firstTry = true;
    }else{
      JOptionPane.showMessageDialog(null,
      "You must keep rolling. If you roll a " + sum + " again, you win. If you roll a 7 before that, you lose.");

      while(firstTry == false){
        int secRoll1 = die1.roll();
        int secRoll2 = die2.roll();
        int secSum = secRoll1 + secRoll2;

        JOptionPane.showMessageDialog(null, "You rolled a " + secRoll1 + " and a " + secRoll2 + ".\n\nThe sum is " + secSum);

        if(secSum == sum){
          JOptionPane.showMessageDialog(null, "You matched!!!");
          winCount++;
          break;
        }

        if(secSum == 7){
          JOptionPane.showMessageDialog(null, "I'm sorry, you rolled a 7.");
          loseCount++;
          break;
        }
      }
    }

    //Shows the user how many times they win or lose
    JOptionPane.showMessageDialog(null, "Your current score..." + "\nwins: " + winCount + "\nlosses: " + loseCount);
  }

}
public class Die 
{
    private int sides;
    private int total;
    private int rolls;

    //The following method is called a 'Constructor'
    //it runs when a new 'Die' is created

    public Die(int numSides) 
    {
        sides=numSides;
        total=0;
        rolls=0;
    }

    //gets a die roll
    public int roll()
    {
        int result = (int)(Math.random()*sides+1);

        total = total+result;
        rolls++;

        return result;
    }

    // The following methods return information about the die

    public int getSides()
    {
        return sides;
    }

    public int getTotal()
    {
        return total;
    }

    public int getRolls()
    {
        return rolls;
    }

}
5f0d552i

5f0d552i1#

int winCount = 0;
int loseCount = 0;

您的变量是在playcraps方法中声明的,因此每次退出/调用该方法时它们都会“丢失”和“重新创建”(正如您所猜测的)。要解决此问题,您必须使其全球化:

public class Craps{
  public static int winCount=0;
  public static int loseCount=0;

  <your other methods like main and play>
}

这将使他们可以从任何地方,因此他们的价值将增加,因为用户不断发挥。

aiqt4smr

aiqt4smr2#

每次你 playCraps(); 函数被调用,您的计数被重置。
你可以换成

//counts the amount of times the player wins or loses
 static int winCount = 0;
 static int loseCount = 0;

静态将防止每次重置计数。但是,如果你打算添加其他玩家,那就行不通了。
在这种情况下,您可以更改您的结构,这样您就有了一个类成员,而不是为每个玩家递增。

ubbxdtey

ubbxdtey3#

您确实提到了原因,分配计数的范围在每个调用中都是相同的,因此您只需从头开始重新开始计数。
在craps类本身中,将相同的tracker counts定义为私有字段并使用它们,因此您只需要将它们移动到类范围;像这样的。

public class Craps {
   private static int winCount = 0;
   private static int loseCount = 0;
}

有关这个简单教程中的更多信息,请尝试阅读java中的变量范围

相关问题