【问题标题】:How do I use the values of variables that are calculated inside a method, in a class?如何在类中使用在方法内部计算的变量值?
【发布时间】:2014-02-22 23:56:39
【问题描述】:

我对编程很陌生,不太了解。我一直在尝试构建一个简单的游戏,用户和计算机通过掷骰子来获得积分。我的方法贴在下面。电脑每回合只能获得20分。

我的问题是,在调用并完成方法后,我需要记住变量 computerTotal 的值。我想确保无论何时完成 computerTurn 方法,我都可以在该方法之外使用计算出的变量 computerTotal。

我尝试在 .java 文件类中建立一个新的 int(但在方法之外),然后在方法中使用该 int 来保存该值,但是我收到关于整数需要为静态的错误?

这一切都让我很困惑。谁能帮帮我?

public static void computerTurn() {

    System.out.println("Passed to Computer.");

    Die computerDie1, computerDie2;
    int computerRound, computerTotal;
    computerRound = 0;
    computerTotal = 0;


    while (computerTotal < 21){
    computerDie1 = new Die();
    computerDie2 = new Die();
    computerDie1.roll();
    computerDie2.roll();

    System.out.println("\n" + "CPU Die One: " + computerDie1 + ", CPU Die Two: " + computerDie2 + "\n");
    computerRound = computerDie1.getFaceValue() + computerDie2.getFaceValue();

    int cpuDie1Value;
    int cpuDie2Value;

    cpuDie1Value = computerDie1.getFaceValue();
    cpuDie2Value = computerDie2.getFaceValue();

    System.out.println ("Points rolled this round for the Computer: " + computerRound);

    computerTotal = computerTotal + computerRound;

    System.out.println ("Total points for the Computer: " + computerTotal + "\n");
    }

【问题讨论】:

    标签: java methods dice


    【解决方案1】:

    在方法内创建的任何变量都是“局部变量”,这意味着它们不能在方法外使用。在方法之外放置一个静态变量来创建可以在任何地方使用的“全局变量”。

    【讨论】:

      【解决方案2】:

      为你的类添加一个方法

      public static int getComputerTotal() { return ComputerTotal;}
      

      然后您可以通过执行以下操作来获取类之外的值:

      ComputerTurn();
      ComputerTurn.getComputerTotal();
      

      【讨论】:

        【解决方案3】:

        把变量放到方法外是对的,但是由于这个方法是static(意味着它不能访问依赖于对象实例的变量),所以只能访问静态变量。在类中声明computerTotal,在方法之外,使用以下代码:

        private static int computerTotal;
        

        您可能应该对面向对象编程以及static 在 Java 中的含义进行一些研究。

        【讨论】:

          【解决方案4】:

          将 computerTotal 声明为您的类的成员变量,以便它的值即使在函数之外也可用。

          class MyClass{
              private int computerTotal ;
          
              public  void function myFunction()
              {
                   ........
                   ......... // your calculations
                   computerTotal = computerTotal + computerRound;      
          
              }
          
             public int getComputerTotal(){return     computerTotal ;}
          

          }

          【讨论】:

            【解决方案5】:

            您必须在任何方法之外声明计算机总数以保留它们。像这样:

            public class name {
                int computertotal = 0; //v=can just uuse int computertotal;
                public void method() {
                     while(computertotal < 20) {
                            computertotal += 1;
                     }
                }
            }
            

            现在他的变量被保存了!

            【讨论】:

              【解决方案6】:

              您可能需要添加一些 setter 和 getter 才能从另一个类中获取该 int。

              class NewClass {
                 private int yourInt = 1;
              }
              

              它告诉你让它成为一个静态变量,因为你可能在像

              这样的语句中调用它
              NewClass.yourInt;
              

              ,静态变量是与一个类相关联的变量,而不是该类的对象。

              Setter 和 getter 是允许您检索或设置来自另一个类的私有值的方法。您可能希望将它们添加到声明 int 的 NewClass 中。 Setter 和 getter 看起来像这样。

              二传手:

              public void setYourInt(int newInt) {
                 this.yourInt = newInt;
              }
              

              吸气剂:

              public int getYourInt() {
                 return this.yourInt;
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-05-07
                • 2013-02-19
                • 1970-01-01
                • 1970-01-01
                • 2023-03-25
                • 2019-11-19
                • 2020-12-27
                • 1970-01-01
                相关资源
                最近更新 更多