美文网首页
数组练习

数组练习

作者: 李哈哈_bing | 来源:发表于2017-12-23 19:03 被阅读0次

    计算全班学员的平均分

    方法一
         int[] a = {60, 70, 80, 90};
            int b = 0;
            int s = 0;
            for (int i = 0; i < a.length; i++) {
                b = a[i];
                s += a[i];
                System.out.println(b);
            }
    
            System.out.println("平均成绩是:" + s / a.length);
    
    方法二
      Scanner scanner = new Scanner(System.in);
            double[] a = new double[5];
            double d=0;
            for (int i = 0;i<a.length;i++){
                System.out.println("输入成绩");
                double g=scanner .nextInt();
                a[i]=g;
                d+=a[i];
            }
            System.out.println("平均分是"+d/a.length);
    

    有一个数列:8,4,2,1,23,344,12 循环输出数列的值,求数列中所有数值的和猜数游戏:从键盘中任意输入一个数据,判断数列中是否包含此数

     Scanner scanner = new Scanner(System.in);
      int[] a = {8, 4, 2, 1, 23, 344, 12};
            int b = 0;
            System.out.println("输入一个数");
            int c = scanner.nextInt();
            int i;
            for (i = 0; i < a.length; i++) {
                b += a[i];
                if (c == a[i]) {
                    System.out.println("包含");
                    break;
                }
            }
            if (i == a.length) {
                System.out.println("不包含");
            }
            System.out.println(b);
    

    从键盘输入本次Java考试五位学生的成绩,求考试成绩最高分

       Scanner scanner = new Scanner(System.in);
      int[] a = new int[5];
            int max = 0;
            int i;
            for (i = 0; i < a.length; i++) {
                System.out.println("输入成绩");
                int g = scanner.nextInt();
                a[i] = g;
                if (max <= a[i]) {
                    max = a[i];
                }
            }
            System.out.println("最大成绩是" + max);
    

    有一组学员的成绩{50,90,70,80},将它们按升序排列。要增加一个学员的成绩,将它插入成绩序列,并保持升序。

    int[] a = {50, 90, 70, 80};
            Arrays.sort(a);
            System.out.println("数");
            int b = scanner.nextInt();
            int c = 0;
            for (int i = 0; i < a.length; i++) {
                if (b <= a[i]) {
                    c = i;
                    break;
                }
            }
            int[] d = new int[5];
          //插入第一步  for (int i = 0; i < c; i++) {
                d[i] = a[i];
            }
         //第二部   d[c] = b;
           //第三部 for (int i = c + 1; i < d.length; i++) {
                d[i] = a[i - 1];
            }
           //遍历 for (int i = 0; i < d.length; i++) {
                System.out.println(d[i]);
            }
    
    

    将 一组乱序的字符进行排序 进行升序和逆序输出

           char[] a={'s','d','b','h'};
                    Arrays.sort(a);
            for (int i = 0;i<a.length;i++){
                System.out.println(a[i]);
            }
                    for (int i = a.length-1;i>=0;i--){
                         System.out.print(a[i]);
            }
    
    

    定义一个字符串数组,查找某个字符串在数组中出现的次数

      Scanner scanner = new Scanner(System.in);
      String[] a = {"ww", "dd", "ww", "aa", "dd", "ww"};
            int c = 0;
            System.out.println("输入字符串");
            String b = scanner.next();
            for (int i = 0; i < a.length; i++) {
    
                if (b.equals(a[i])) {
                    c++;
                }
    
            }
            System.out.println(b + "出现" + c + "次");
      
    

    相关文章

      网友评论

          本文标题:数组练习

          本文链接:https://www.haomeiwen.com/subject/lujqgxtx.html