Вот вам класс Die, о котором вы просили ... Алекс не мог бы вам подробнее рассказать об использовании .getValue в моем классе Die?Сумма списка 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]);
}
}
Знание класса Die помогло бы. –
Могу ли я спросить, почему вы используете ArrayList и Array, делая то же самое с ними обоими? –
Рассмотрите возможность свернуть первые 4 для петель в одну петлю с четырехпозиционным телом, гораздо более сжатым и читаемым ... – RobP