Introduction to Java Programming编程题6.3<计算数字的出现次数>

/*
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.");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值