다음은 요청한 다이 클래스입니다 ... Alex는 내 다이 클래스에서 .getValue를 사용하는 것에 대해 더 자세히 설명 할 수 있습니까? 내 배열과 ArrayList에의 합을 찾기 위해 다른 코드를 사용하여 시도했습니다배열과 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;
}
}
는 ... 여기 내 코드입니다. 누군가가 어떻게 끝을 해결할 수 있는지 말해 줄래?
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]);
}
}
다이 클래스를 아는 것이 도움이됩니다. –
ArrayList와 Array를 사용하는 이유는 무엇입니까? –
처음 4 개의 for 루프를 4 개의 명령문 본문이있는 하나의 루프로 축소하는 것을 고려하십시오. 훨씬 더 압축되고 읽을 수 있습니다. – RobP