【发布时间】:2015-05-14 09:32:25
【问题描述】:
在我的程序中调用 record() 方法。所以如果用户输入 1、2、3、4、5、6、7。按此顺序,它会打印出 7、6、5、4、3、2、1。我尝试了增强的 for 循环、递增的 for 循环等。请帮助,提前谢谢。
ArrayList<Integer> playerMoves = new ArrayList<Integer>(20);
public void record(int value) {
playerMoves.add(0, value);
if(playerMoves.size() > 20) {
playerMoves.remove(playerMoves.size() - 1);
}
}
public void displayPlayerMoves() {
int fullstopCount = 20;
System.out.print(playerMoves.size() + " moves: ");
for(int i = playerMoves.size(); i > 0; i--) {
fullstopCount--;
if(playerMoves.size() == fullstopCount) {
System.out.print(playerMoves.get(i) + ".");
} else {
System.out.print(playerMoves.get(i) + ", ");
}
}
}
【问题讨论】:
-
你正在使用
fullstopCount它被初始化为0然后你正在使用fullstopCount--为什么?您还需要将add方法更改为playerMoves.add(value);
标签: java arrays sorting arraylist