/*
Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
2 occurs 2 times.
3 occurs 1 times.
4 occurs 1 times.
5 occurs 2 times.
6 occurs 1 times.
23 occurs 1 times.
43 occurs 1 times.
*/
import java.util.Scanner;
import java.util.Arrays;
public class OcussDigitSeconds {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int SIZE = 20;
int i = 0;
int[] number = new int[SIZE];
int temp;
System.out.print("Enter the integers between 1 and 100: ");
while (i < SIZE && (temp = input.nextInt()) != 0) {
if (temp > 0 && temp < 100)
number[i++] = temp;
}
final int LEN = i;
Arrays.sort(number, 0, LEN);
int index = 0;
int[] times = new int[LEN];
for (i = 0; i < LEN; i++) {
times[index] = 1;
while (number[i] == number[i + 1]) {
times[index]++;
i++;
}
index++;
}
for (i = 0, index = 0;i < LEN; i += times[index], index++)
System.out.println(number[i] + " occurs " + times[index] + " times.");
}
}
Introduction to Java Programming编程题6.3<计算数字的出现次数>
最新推荐文章于 2024-04-03 12:02:34 发布