【问题标题】:static methods can not run in a non-static context [duplicate]静态方法不能在非静态上下文中运行[重复]
【发布时间】:2020-06-01 05:25:44
【问题描述】:

我正在尝试通过使用各种方法来掷骰子、比较和总结骰子来模拟 Yahtzee 游戏。

这是一项作业,我需要使用这三种静态方法来完成我的所有工作。

当我尝试运行我的代码时,我得到了这个错误

exit status 1
Main.java:6: error: non-static method rollDice(int[]) cannot be referenced from a static context
   rollDice(dice);
   ^
Main.java:7: error: non-static method rollDice(int[]) cannot be referenced from a static context
   dice= rollDice(dice);
         ^
2 errors  

我尝试在将它们返回的值分配给变量之前运行这些方法,但这似乎不起作用。这是我的完整代码:

class Main {
  public static void main(String[] args) {
  // int array to hold 5 dice

   int dice[] = new int[5];
   rollDice(dice);
   dice= rollDice(dice);

   fiveOfaKind(dice);
   int points = fiveOfaKind(dice);

   getChance(dice,points);
   int printpoint = getChance(dice,points);
   // You may roll the dice up to 3 times to try to get 5 of a Kind 
   // If you get 5 of a Kind, stop if not keep trying.
   // If you do not get 5 of a Kind after the 3rd roll, you must take the 
   // CHANCE score. 

   System.out.println("Your score is " + printpoint);



  }// end of main method

  public int[] rollDice(int dice[]){
    // generate 5 random numbers / update dice array
     for (int i = 0; i < dice.length; i++) {
      dice[i] = (int)(Math.random() * 6 + 1);
     }
    return dice;

  }// end of rollDice - rolls 5 dice and puts them in an array
   // All 5 dice must be rolled each time

  public static int fiveOfaKind(int dice[]){
     int pts = 0;
     int rNum=0;

    for(int i=0; rNum<3; rNum++){
    if(dice[0]==dice[1]&&dice[1]==dice[2]&&dice[2]==dice[3]&&dice[3]==dice[4]&&dice[4]==dice[5]&&dice[5]==dice[6]){
      pts=50;
    }
    else if(rNum==3){
      pts=0;
    }
    else{
      int[] rollDice;
    }

    }//end of forloop

    // use Array dice - evaluate if all elements are equal
    // if true = pts = 50
    // if false - pts = 0


    return pts;

  }// end of fiveOfaKind - evaluates the dice roll to see if all 5 are equal
   // Returns 50 points if they do

  public static int getChance(int dice[], int points){

    if(points<50){
       points=0;
      for (int i = 0; i < dice.length; i++) {
         points+=dice[i];
      }
    }
    // use Array dice - sum the elements of the Array
    // pys = calculated value of the sum

    int p = points;

    return p;

  }// end of getChance - adds the total value of the dice
    // Returns the total points calculated


}// end of  class

【问题讨论】:

  • 将rollDice()设为静态,否则您需要class Main的实例才能调用它。
  • 您已经完全从头到尾阅读了错误消息。再试一次。

标签: java methods static dice


【解决方案1】:

您需要将rollDice() 方法设为静态。 不能从静态方法调用非静态方法。如果要调用非静态方法,则必须使用类的实例来调用该方法。

【讨论】:

    【解决方案2】:

    简单地说,静态方法/字段更像是与类关联的属性,可以在类的对象之间共享。

    public int[] rollDice 方法不存在,除非您创建包含该方法的类的对象,因为它是非静态的。非静态方法只是与您创建的每个对象相关联。因此,您创建的对象将拥有自己的一组不依赖于其他对象的属性,您可以使用这些属性来维护该对象内的状态。

    简单修复:

    将方法 rollDice 设为静态(根据您的程序结构推荐)

    或

    这样调用方法:

    Main obj = new Main();
    obj.rollDice(dice);
    

    如果您不知道两者之间的区别,我建议您阅读这篇文章 (Static vs Non-Static)。从面向对象编程的角度理解它真的很重要。

    【讨论】:

      【解决方案3】:

      静态方法是通用的,并且对于所有对象和它们共享的类都是相同的,并且对于它们来说,使该类的对象仅通过使用类调用函数就足够了,但另一方面对于非静态方法,制作该类的对象很重要,因为它们与对象相关联并且只能使用该类的对象调用

      所以你需要做一个Main类的函数,然后用它来调用rollDice函数

      例如:
      Main ob1=new Main();

       ob1.rollDice(dice);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-31
        • 2023-03-22
        • 2016-09-17
        • 1970-01-01
        • 1970-01-01
        • 2011-09-10
        相关资源
        最近更新 更多