2014-10-29 52 views
-1

嗨,我很確定這是一個初學JAVA問題。我跑了我的編譯器,它顯示有錯誤試圖從另一個類調用方法而不是獲取錯誤。 JAVA

Cannot make a static reference to the non-static method roll() from the type Die 

在我的主要方法的最後一行。

我想要做的是滾動兩個骰子並添加它們。我的問題是這條線有什麼問題
以及我該如何解決這個問題?在此先感謝

 /** main method 

     Die myDie1 = new Die(); 
     Die myDie2 = new Die(); 



     for(int roll=1;roll<=total;roll++) 
      { 
       counts[(Die.roll()+Die.roll())]++; //<--error here 
      } 
     **/ 

骰子方法

public class Die 
    { 
     private final int MAX = 6; // maximum face value 

     private int faceValue; // current value showing on the die 

     //----------------------------------------------------------------- 
     // Constructor: Sets the initial face value. 
     //----------------------------------------------------------------- 
     public Die() 
     { 
     faceValue = 1; 
     } 

     //----------------------------------------------------------------- 
     // Rolls the die and returns the result. 
     //----------------------------------------------------------------- 
     public int roll() 
     { 
     faceValue = (int)(Math.random() * MAX) + 1; 

     return faceValue; 
     } 

     //----------------------------------------------------------------- 
     // Face value mutator. 
     //----------------------------------------------------------------- 
     public void setFaceValue (int value) 
     { 
      faceValue = value; 
     } 

     //----------------------------------------------------------------- 
     // Face value accessor. 
     //----------------------------------------------------------------- 
     public int getFaceValue() 
     { 
      return faceValue; 
     } 

    //----------------------------------------------------------------- 
    // Returns a string representation of this die. 
    //----------------------------------------------------------------- 
     public String toString() 
    { 
     String result = Integer.toString(faceValue); 

     return result; 
    } 
    } 
+0

可能重複的[不能靜態引用非靜態方法](http://stackoverflow.com/questions/4969171/cannot-make-static-reference-to-non-static-method) – 2014-10-29 06:02:08

回答

0

你必須創建Die類型的對象或讓你method static。也許你應該看看靜態類和變量以及Java對象及其差異。

一種方法是:

變化:

public int roll() 
{ 
     faceValue = (int)(Math.random() * MAX) + 1; 

     return faceValue; 
} 

要:

public static int roll() 
{ 
     faceValue = (int)(Math.random() * MAX) + 1; 

     return faceValue; 
} 

更好的和Java面向對象的方法是從

for(int roll=1;roll<=total;roll++) 
{ 
    counts[(Die.roll()+Die.roll())]++; 
} 

更改爲:

for(int roll=1;roll<=total;roll++) 
{ 
    counts[(myDie1.roll()+myDie2.roll())]++; 
} 
+0

有一個在數組索引中丟失左括號 – Chiseled 2014-10-29 04:53:38

+0

Downvoter,謹慎解釋? – dazito 2014-10-29 04:56:15

+1

沒有類'Roll'的類' – 2014-10-29 04:57:05

1

更改線路

counts[(Die.roll()+Die.roll())]++; 

counts[myDie1.roll()+ myDie2.roll()]++; 

注意存在丟失的左括號太

+1

你確定缺少左括號嗎? – 2014-10-29 04:54:09

+0

只是給了重複的答案..你得到它第一個:) – CharlieS 2014-10-29 04:54:25

+0

是的,爲什麼加上額外的(,他們平衡.. – CharlieS 2014-10-29 04:56:08

0

你可能只是想取代:

counts[(Die.roll()+Die.roll())]++; 

有:

counts[(myDie1.roll()+myDie2.roll())]++; 

調用對象,不是非實例方法實例化方法的類

0

這是因爲ü給靜態方法參照到非靜態對象。

根據java你不能直接調用方法直接引用。所以你需要使這個方法是靜態的。

所以根據這個改變你的方法。

public static int roll() 
     { 
     faceValue = (int)(Math.random() * MAX) + 1; 

     return faceValue; 
     } 

後更新此這將工作

for(int roll=1;roll<=total;roll++) 
      { 
       counts[(Die.roll()+Die.roll())]++; //<--error here will be solved 
      } 

希望這會有所幫助。