2014-12-12 31 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)); 
}