【问题标题】:How would I keep a count value in my function?我将如何在我的函数中保留计数值?
【发布时间】:2021-03-10 14:20:03
【问题描述】:

我需要制作一个模拟掷骰子游戏的程序。但我遇到的问题是计算玩家赢或输的次数。我认为这是因为由于计数不在 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;
    }
   
}

【问题讨论】:

  • 是的...移动你的计数并将它们设置为类变量——它们不应该是方法的本地变量

标签: java counting


【解决方案1】:
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>
}

这将使它们可以从任何地方访问,因此它们的价值会随着用户继续玩而增加。

【讨论】:

    【解决方案2】:

    你确实提到了原因,每次调用分配计数的范围都是一样的,所以你只需从头开始计数。

    在您的 Craps 类本身中,将相同的跟踪器计数定义为私有字段并改为使用它们,因此您只需将它们移动到类范围;像这样的。

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

    有关此简单教程的更多信息,请尝试阅读此内容 Variables Scope in Java

    【讨论】:

      【解决方案3】:

      每次调用您的 playCraps(); 函数时,您的计数都会重置。

      你可以改成

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

      静态将防止每次重置计数。 但是,如果您打算添加其他无法使用的播放器。

      在这种情况下,您可以更改您的结构,以便您拥有一个类成员,而不是为每个玩家递增。

      【讨论】:

      • 这是一种可能的方法,但在大多数情况下不是一个很好的方法。
      • 完全同意你@klutt
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 2023-01-15
      • 1970-01-01
      • 2023-01-19
      相关资源
      最近更新 更多