zoukankan      html  css  js  c++  java
  • 第6次JAVA

    1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。

    import java.util.*;
    
    public class jkk {
        public static void main(String[] args) {
            int[] arr = new int[] { 10, 20, 30, 40, 50 };
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
        }
    }

     2.编写一个简单程序,要求数组长度为5,动态赋值10,20,30,40,50,在控制台输出该数组的值。

    import java.util.*;
    
    public class jkk {
        public static void main(String[] args) {
            int[] a = new int[5];
            a[0] = 10;
            a[1] = 20;
            a[2] = 30;
            a[3] = 40;
            a[4] = 50;
            for (int i = 0; i < a.length; i++) {
                System.out.println(a[i]);
            }
        }
    }

     3.编写一个简单程序,定义整型数组,里面的元素是{23,45,22,33,56},求数组元素的和、平均值

    import java.util.*;
    
    public class jkk {
        public static void main(String[] args) {
            int a[] = { 23, 45, 22, 33, 56 };
            double sum = 0;
            for (int i = 0; i < a.length; i++) {
                sum += a[i];
            }
            System.out.print("和" + sum + "平均值" + sum / 5);
        }
    }

     4.在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。

    import java.util.*;
    
    public class jkk {
        public static void main(String[] args) {
            int a[] = { 18, 25, 7, 36, 13, 2, 89, 63 };
            int max = a[0];
            int index = 0;
            for (int i = 1; i < a.length; i++) {
                if (max < a[i]) {
                    max = a[i];
                    index = i;
                }
            }
            System.out.print("最大数是" + max + "下标是" + index);
        }
    }

     5. 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)

    import java.util.*;
    
    public class jkk {
        public static void main(String[] args) {
            int[] a = new int[] { 1, 2, 3, 4, 5 };
            int b;
            for (int i = 0; i < a.length / 2; i++) {
                b = a[i];
                a[i] = a[a.length - 1 - i];
                a[a.length - 1 - i] = b;
            }
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + ", ");
            }
    
        }
    
    }

  • 相关阅读:
    float浮动后,父级元素高度塌陷和遮盖问题
    Json
    测试连接数据库是否成功
    spark standalone zookeeper HA部署方式
    用NAN简化Google V8 JS引擎的扩展
    在Android上使用Google V8 JS 引擎
    数据可视化工具zeppelin安装
    kafka0.8.2以下版本删除topic
    kafka迁移数据目录
    scala2.10.x case classes cannot have more than 22 parameters
  • 原文地址:https://www.cnblogs.com/horfe666/p/12666044.html
Copyright © 2011-2022 走看看