zoukankan      html  css  js  c++  java
  • java-学习7

          数组的定义及使用

    public class TestArr {
        public static void main(String[] args) {
            //声明数组
            double array1[];
            double[] array2;
            double[] array3,array4,array5;
            //分配内存空间
            array1=new double[5];
            array1[0]=0;
            array1[1]=1;
            array1[2]=3;
            array1[3]=23;
            System.out.println(array1[3]);
        }
    }

      输出:

    23.0

           范例:学生管理系统

    public class arryTest2 {
        public static void main(String[] args) {
        //学生管理系统
            int student;                     //控制学生变量
            double sum=0,avg=0;              //总成绩、平均成绩
            double[] temp=new double[10];    //创建新数组,附长度
            //创建一个Scanner类的对象,用它来获得用户的输入
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入10名学生成绩");
            
            for(student=0;student<temp.length;student++) {
                //读取用户的输入
                temp[student]=sc.nextDouble();
                sum+=temp[student];
                
            }
            
            avg=sum/10;
            System.out.println("平均值是:"+avg);
            
            for(student=0;student<temp.length;student++) {
                
                if(temp[student]<avg) {
                    System.out.println("同学的成绩"+temp[student]+"成绩低于平均成绩");
                }else if(temp[student]>avg) {
                    System.out.println("同学的成绩"+temp[student]+"成绩高于平均成绩");
                }else{
                    System.out.println("同学的成绩"+temp[student]+"成绩等于平均成绩");
                }
            }
        }
    }

      输出:

    请输入10名学生成绩
    52  32  56  99  89  78  56  23  59  89
    平均值是:63.3
    同学的成绩52.0成绩低于平均成绩 同学的成绩32.0成绩低于平均成绩 同学的成绩56.0成绩低于平均成绩 同学的成绩99.0成绩高于平均成绩 同学的成绩89.0成绩高于平均成绩 同学的成绩78.0成绩高于平均成绩 同学的成绩56.0成绩低于平均成绩 同学的成绩23.0成绩低于平均成绩 同学的成绩59.0成绩低于平均成绩 同学的成绩89.0成绩高于平均成绩

           范例:数组应用

    public class arryTest3 {
        public static void main(String[] args) {
            int score[] = {67,89,87, 69,90,100,75,90};
            int max=0;
            int min=0;
            max=min=score[0];
            
            for(int x=0;x<score.length;x++) {
                if(score[x]>max) {
                    max=score[x];
                }
                if(score[x]<min) {
                    min=score[x];
                }
            }
                System.out.println("最高成绩:"+max);
                System.out.println("最低成绩:"+min);
            }
            
    }

      输出:

    最高成绩:100
    最低成绩:67

      范例:冒泡对比大小

    public class arryTest4 {
        public static void main(String[] args) {
            int score[]= {67,89,87,69,90,100,75,90};
            for(int i=1;i<score.length;i++) {
                for(int j=0;j<score.length;j++) {
                    if(score[i]<score[j]) {
                        int temp = score[i];
                        score[i] = score[j];
                        score[j] = temp;
                    }
                }
            }
            for (int i=0;i<score.length;i++) {
                System.out.println(score[i]+"	");
            }
        }
    }

      输出:

    67    69    75    87    89    90    90    100

      二维数组的定义及使用

    public class twoArry {
        public static void main(String[] args) {
            int score[][] = new int[4][3];
            score[0][0]=1;
            score[0][1]=31;
            score[0][2]=32;
            
            score[1][0]=2;
            score[1][1]=78;
            score[1][2]=34;
            
            score[2][0]=3;
            score[2][1]=89;
            score[2][2]=34;
            
            score[3][0]=4;
            score[3][1]=90;
            score[3][2]=34;
            
            for (int i = 0; i<score.length;i++) {
                for(int j = 0;j<score[i].length;j++) {
                    System.out.println(score[i][j]);
                }
                System.out.println("");
            }
        }
    }

      输出:

    1     31     32
    
    2     78     34
    
    3     89     34
    
    4     90     34

      范例:二维数组2

    public class towArry2 {
        public static void main(String[] args) {
            //静态初始一个二维数组,每行的数组元素个数不一样
            int score[][] = {{1,23,23,45},{2,34,45,45,34},{3,345,347,231,453},{4,456,987,123,32,32}};
            for (int i = 0;i<score.length;i++) {
                for(int j=0; j<score[i].length;j++) {
                    System.out.println(score[i][j]+"	");
                }
                System.out.println("");
            }
        }
    }

      输出:

    1    23    23    45    
    
    2    34    45    45    34    
    
    3    345    347    231    453    
    
    4    456    987    123    32    32    

      三维数组

    public class threeArry {
        public static void main(String[] args) {
            //定义一个三维数组,使用静态初始化的方式
            int score[][][] = {{{5,1},{6,7}},{{9,4},{8,3}}};
            for(int x = 0;x<score.length;x++) {//第一层循环
                for(int y = 0;y<score[x].length;y++) {//第二层循环
                    for(int z = 0;z<score[x][y].length;z++) {//第三层循环
                        System.out.println("score["+x+"]["+y+"]["+z+"]="+score[x][y][z]);
                    }
                }
                
            }
        }
    }

      输出:

    score[0][0][0]=5
    score[0][0][1]=1
    score[0][1][0]=6
    score[0][1][1]=7
    score[1][0][0]=9
    score[1][0][1]=4
    score[1][1][0]=8
    score[1][1][1]=3
  • 相关阅读:
    Go语言指针
    程序员 需要掌握得600个英语单词
    Go语言管道
    Go语言容器
    Go语言切片
    Go语言类型转换
    Go语言数组
    LR静态存储/动态存储/指针变量脚本说明
    MQ报错2009/2085解决方法
    Windows性能监控监视器(perfmon使用)
  • 原文地址:https://www.cnblogs.com/liaohongwei/p/9888863.html
Copyright © 2011-2022 走看看