2014-12-12 32 views
0

這是您要求的Die類... Alex您是否可以進一步解釋在我的Die類中使用.getValue?Array和ArrayClass列表的總和


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; 
    } 
} 

我使用不同的代碼來找到我的數組和ArrayList的總和嘗試...這裏是我的代碼。有人可以告訴我如何解決這個問題。

import java.util.ArrayList; 


public class Driver { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     final int SIZE = 10; 
     Die sumArray; 
     Die sumArrayList; 


     Die[] DieList = new Die[SIZE]; 

     ArrayList<Die> DieList2 = new ArrayList<Die>(); 

     for (int i = 0; i < SIZE; i++) 
     { 
      DieList[i] = new Die(); 
     } 

     for (int i = 0; i < SIZE; i++) 
     { 
      DieList2.add(new Die()); 
     } 

     for (int i = 0; i < SIZE; i++) 
     { 
      DieList[i].roll(); 
     } 

     for (int i = 0; i < SIZE; i++) 
     { 
      DieList2.get(i).roll(); 
     } 

     System.out.print("Array: "); 

     for (int i = 0; i < SIZE; i++) 
     { 
     System.out.print(DieList[i] + " "); 
     } 

     System.out.println(); 

     System.out.println("ArrayList: " + DieList2); //.get(index) specific index value in the arrayList 

     for (Die i : sumArray){ 
      sumArray+= i; 
     } 

    // sumArrayList = (DieList2(0) + DieList[1] + DieList[2] + DieList[3] + DieList[4] + DieList[5] + DieList[6] + DieList[7] + DieList[8] + DieList[9] + DieList[10]); 

    } 
} 
+0

瞭解die類將有所幫助。 – 2014-12-12 23:45:38

+1

我可以問爲什麼你在使用ArrayList和Array的時候同時使用它們呢? – 2014-12-12 23:46:11

+0

考慮將前四個循環合併成一個循環,並使用四個語句的主體,更加簡潔和易讀... – RobP 2014-12-12 23:48:39

回答

0

我看你missunderstanding一些概念:

  • 在對象類(從其中所有的類繼承的類)中定義的印刷方法將打印對象,而不是任何屬性的參考。這應該是顯而易見的,因爲print方法無法知道要打印哪個屬性(屬性和引用的列表都不存儲在一個對象中)。如果需要打印其內部屬性,則需要專門編寫一個方法來覆蓋打印方法,但如果您沒有足夠的理由來覆蓋預定義的方法,則不要執行此操作。稱它爲getValue(),即

  • sumArray只是一個對象,而不是對象列表。循環遍歷第一個for循環的方式是隻讀取一個對象,因爲sumArray是您的Die對象之一,而不是列表。

  • 除了字符串,operator +僅用於基本類型(int,boolean,float,...)。如果+ =沒有破壞你的編譯,我知道編譯器正在將操作解釋爲一個單獨的賦值操作,因此你在說sumArray的引用現在再次指向... sumArray。聲明是無用的。

  • 至於最後一行評論,你不能總結上述原因的對象。一個對象是一個複雜的實體化合物,由許多屬性和方法組成,編譯器無法知道如何將這些實體默認進行求和。如果您因故由C抵達到Java ++中進行awared,你不能重載操作(但肯定這不是你的情況)

所以,如果你想辦法解決

  • 定義的getValue()方法在您的Die類中讀取存儲的值。我假設你已經正確編碼了滾動值來生成一個值並存儲它。

要從陣列總結所有的值做:

int sums1=0;  
for (Die i : DieList){ 
sums1+= i.getValue(); 
} 

要從ArrayList中總結的所有值做:

int sums2=0;  
for (int i=0;i<DieList2.size();i++){ 
    sums2+= DieList2.get(i).getValue(); 
} 

最後打印它們:

System.out.println("Sum of array elements: "+sums1); 
System.out.println("Sum of ArrayList elements: "+sums2); 
0

如果你只是試圖找到滾動後數組的總和骰子你可以縮短你的代碼!

你可以像這樣在一個循環中壓縮所有添加,滾動和加總你的骰子。

public static void main(String[] args) { 
    final int SIZE = 10; 
    int sumList1 = 0; 
    int sumList2 = 0; 
    Die[] DieList = new Die[SIZE]; 
    ArrayList <Die> DieList2 = new ArrayList <Die>(); 

    for (int i = 0; i < SIZE; i++) { 
     DieList[i] = new Die(); 
     DieList2.add(new Die()); 
     DieList[i].roll(); 
     DieList2.get(i).roll(); 
     sumList1 += DieList[i].getFaceValue(); 
     sumList2 += DieList2.get(i).getFaceValue(); 
    } 

    System.out.println("Sum of Array: " + sumList1); 
    System.out.println("Sum of ArrayList: " + sumList2); 
    System.out.println("Sum of both: " + (sumList1 + sumList2)); 
}