我只是试图通过Count对选项卡式输出进行排序,并对所有其他列进行适当的排序。
int maxNum = A6DiceRolling.diceSides;
Scanner sc = new Scanner(System.in);
int rollnum;
int randomValue;
NumberFormat formatter = new DecimalFormat("#0.00");
ArrayList<Integer> numbers = new ArrayList<Integer>();
ArrayList<Integer> counts = new ArrayList<Integer>();
//Using ArrayList for my Sum and Counts
System.out.println("Welcome to the Dice Roll Stats Calculator!");
System.out.println("Enter amount of rolls: ");
rollnum = sc.nextInt();
for (int i = 0; i < rollnum; i++) {
randomValue = (1 + (int) (Math.random() * maxNum)) + (1 + (int) (Math.random() * maxNum));
if (numbers.contains(randomValue)) {
int position = numbers.indexOf(randomValue);
counts.set(position, counts.get(position) + 1);
} else {
numbers.add(randomValue);
counts.add(1);
}
}
System.out.println("Sum\tCount\tPercentage");
System.out.println("----\t---\t----------");
for (int i = 0; i < numbers.size(); i++) {
System.out.println(numbers.get(i) + "\t" + counts.get(i)
+ "\t" + formatter.format(((double) (counts.get(i) * 100)) / rollnum) + "%");
我需要的是一个输出,用于对“计数”列进行排序。 我不熟悉Array的sort方法,但由于ArrayList不同,我不知道从哪里开始查看如何将它实现到我在这里。
我现在得到的是输出:
1000卷6面骰子后
Sum Count Percentage
--- --- ----------
3 63 6.30%
5 116 11.60%
9 93 9.30%
7 167 16.70%
11 59 5.90%
4 85 8.50%
8 139 13.90%
10 90 9.00%
6 138 13.80%
2 27 2.70%
12 23 2.30%
答案 0 :(得分:0)
假设两个列表的大小相同,即numbers
和counts
- 您应该首先使用counts
API对Collections.sort()
列表进行排序,然后迭代{{1 }而不是counts
numbers
有关如何对列表进行排序以及为何与“设置”或“地图”不同的进一步阅读,请阅读此帖 Why is there no SortedList in Java?