我有一阵子了。我尝试调试,但我无法找出解决方案。我正在尝试计算数字的出现。所以我的问题是当我打印输出时
3 occurs 1 times
1 occurs 1 times
0 occurs 1 times
2 occurs 1 times
1 occurs 2 times
3 occurs 2 times
2 occurs 2 times
0 occurs 2 times
10 occurs 1 times
4 occurs 1 times
代替
1 occurs 2 times
0 occurs 2 times
2 occurs 2 times
3 occurs 2 time
10 occurs 1 times
4 occurs 1 times
因此,如果该数字出现超过 1 次,它应该只说一次,而不是出现次数。干杯 这是代码
import java.util.*;
public class CountingOccuranceOfNumbers
{
public static void main(String[] args)
{
countNumbers();
}
public static void countNumbers()
{
Scanner input = new Scanner(System.in);
Random generator = new Random();
int[] list = new int[11];
int[] counts = new int[150];
int counter = 0;
int number = 1;
while(counter <= 10)
{
number = generator.nextInt(11);
list[counter] = number;
counter++;
}
for(int i=0; i<list.length - 1; i++)
{
counts[list[i]]++;
// System.out.print(list[i] + " ");
System.out.println(list[i] +" occurs " + counts[list[i]] + " times");
}
}
}