数组相关练习

1、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};

求出上面数组中0-9分别出现的次数    

public void test1() {
        int[] count = new int[10];
        int[] scores = new int[]{0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};

        for(int i = 0; i < scores.length; i++) {
            count[scores[i]]++;
        }

        for(int i = 0; i < 10; i++) {
            System.out.println(i + "出现的次数是:" + count[i]);
        }
}

2、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};

要求求出其中的奇数个数和偶数个数。

public void test2() {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int sum1 = 0, sum2 = 0;

        for(int i = 0; i < scores.length; i++) {
            if(scores[i] % 2 != 0) {
                sum1++;
            } else {
                sum2++;
            }
        }

        System.out.println("奇数个数:" + sum1 + "  偶数个数:" + sum2);
    }

3、题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

char[] array = {'a' , 'b', 'c', 'b' , 'a'};

public void test3() {
        char[] array = new char[]{'a','b','c','b','a'};

        for(int i = 0; i < array.length / 2; i++) {
            if(array[i] != array[array.length - 1 - i]) {
                System.out.println("不是回文");
                return;
            }
        }

        System.out.println("是回文");
    }

4、输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数。

public void test4() {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        char[] ch = s.toCharArray();

        int alpha = 0, blank = 0, num = 0, other = 0;
        for(int i = 0; i < ch.length; i++) {
            if(ch[i] <= '9' && ch[i] >= '0') {
                num++;
            } else if((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z')) {
                alpha++;
            } else if(ch[i] == ' ') {
                blank++;
            } else {
                other++;
            }
        }

        System.out.println("英文字母的个数:" + alpha + "  空格的个数:" + blank + "  数字的个数:" + num + "  其他字符的个数:" + other);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值