JAVA学习笔记(4)

数组

public class work {
    public static void main(String[] args) {
        int arr[] = new int[3];
        arr[0] = 0;
        arr[1] = 1;
        arr[2] = 2;
        for(int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);
    }
}

冒泡排序

public class work {
    public static void main(String[] args) {
        int arr[] = new int[3];
        arr[0] = 45;
        arr[1] = 65;
        arr[2] = 12;
        for(int i = 0; i < arr.length-1; i++){
            for(int j = 0; j < arr.length-1-i;j++){
                if(arr[j]>arr[j+1]){
                    arr[j] = arr[j] + arr[j+1];
                    arr[j+1] = arr[j] - arr[j+1];
                    arr[j] = arr[j] - arr[j+1];
                }
            }
        }
        for(int i = 0; i < arr.length;i++)
            System.out.println(arr[i]);
    }
}

作业

在这里插入图片描述

public class work {
    public static void main(String[] args) {
        //work1
        int arr[] = new int[100];
        for(int i = 0; i < arr.length; i++){
            arr[i] = i;
            if(arr[i] % 3 == 0)
                System.out.print(arr[i]+" ");
            if(i % 10 == 0)
                System.out.println();
        }

        //work2
        int [][]scort ={{90,91},{20,100},{70,81}};
        int Chinese[] = new int[2];
        int Math[] = new int[2];
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 2; j++)
            {
                if(j==0)
                    Chinese[0] += scort[i][j];
                if(j==1)
                    Math[0] += scort[i][j];
            }
        }
        Chinese[1] = Chinese[0]/3;
        Math[1] = Math[0]/3;
        for(int i = 0; i < 2; i++){
            if(i == 0)
                System.out.print("语文总成绩:"+Chinese[i]+" 数学总成绩:"+Math[i]);
            if(i == 1)
                System.out.print("语文平均分:"+Chinese[i]+" 数学平均分:"+Math[i]);
        }

        //work3
        int []arr3 ={1,2,3,4,5};
        for(int i = arr.length-1; i >= 0;i--)
            System.out.println(arr3[i]);

        //work4
        int arr4[] = new int[10];
        for(int i = 0; i < arr4.length; i++)
            arr4[i] = i;

        //work5
        int Max = arr4[0];
        int Min = arr4[0];
        double Average;
        for(int i = 0; i < arr4.length;i++)
        {
            if(Max < arr4[i])
                Max = arr4[i];
            if(Min > arr4[i])
                Min = arr4[i];
        }
        Average = (Max+Min+0.0)/2;

        //work6
        int max[] = new int[2];
        max[0] = arr4[0];
        for(int i = 0; i < arr4.length;i++)
            if(max[0] < arr4[i])
            {
                max[0] = arr4[i];
                max[1] = i;
            }

        //work7
        int []arr7 ={13,54,65,34,56,5};
        for(int i = 0; i < arr7.length; i++)
            if(arr7[i] == 5)
                System.out.println("数字5在数组中");

        //work8
        int sum=0;
        double average;
        for(int i = 0;i < arr4.length;i++)
            sum += arr4[i];
        average = (sum+0.0)/arr4.length;
    }
}

©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页