// sets up random number of markers in a
// one-dimensional array
// numMarkers markers in a board of size boardSize
public class SimpleDotCom
{
// constants
private final static int DEFAULT_MARKERS = 3;
private final static int DEFAULT_BOARD_SIZE = 10;
// data members
private int[] markers; // stores the marker positions
private int boardSize; // stores the size of the board
private int endOfMarkers;
// default constructor
// 3 markers in a board of 10
public SimpleDotCom()
{
this(DEFAULT_MARKERS, DEFAULT_BOARD_SIZE);
}
// constructor to set up
// numMarkers and boardSize
public SimpleDotCom(int numMarkers, int boardSize)
{
markers = new int[numMarkers];
this.boardSize = boardSize;
endOfMarkers = markers.length - 1;
int i, j, randNum;
int[] original = new int[boardSize];
for (i = 0; i < original.length; i++)
original[i] = i;
// scramble original
for (i = original.length - 1;
i >= original.length - markers.length;
i--)
{
randNum = (int) (Math.random() * (i+1));
// swap original[i] and original[randNum]
j = original[i];
original[i] = original[randNum];
original[randNum] = j;
}
for (i = 0; i < markers.length; i++)
markers[i] = original[i+original.length-markers.length];
} // end SimpleDotCom
// check if the guess is a hit or a miss
// precondition: guess is valid
public String checkYourself(int guess)
{
for (int i = 0; i <= endOfMarkers; i++)
if (markers[i] == guess)
{
markers[i] = markers[endOfMarkers];
endOfMarkers--;
return "Hit";
}
return "Miss";
} // end checkYourself
// returns the number of markers in the game
public int numberOfMarkers()
{
return markers.length;
} // end numberOfMarkers
// returns the size of the board
public int sizeOfBoard()
{
return boardSize;
} // end sizeOfBoard
} // end SimpleDotCom
這是我需要修改的程序。我將數組修改爲ArrayList對象,我不知道該怎麼做。任何信息/愛慕都有幫助。如果你需要知道任何問題,我會讓你知道的。再次感謝您的幫助。如何修改一個Java程序從數組到ArrayList對象?
這個功課? – Neal 2011-03-28 14:32:09
是的,我不會對此撒謊。我只是不知道該怎麼做。這是我來到這裏的唯一原因。我不明白。 – Leasha 2011-03-28 14:34:23
是的,這是作業。我沒有要求任何人爲我做。我所要求的只是建議如何去做。我迷路了,不明白,所以我在努力。我在尋求建議並不壞。沒有人可以知道是否有人願意向需要它的人提供建議。感謝任何人提供adivce。我非常感謝它,它幫助我理解我需要做什麼。 – Leasha 2011-03-28 14:39:53