java基礎作業一

作業一

  1. 給定一個int類型的數組,要求:
    1.求和
    2.求平均數(int)
    3.求最大值,最小值
    4.打印這個數組中數據

代碼如下:

package homework2;

public class ComputeArrayDemo {

    public static void main(String[] args) {
        /**
         * 給定一個int類型的數組,要求:
         * 1.求和
         * 2.求平均數(int)
         * 3.求最大值,最小值
         * 4.打印這個數組中數據
         */
        int[] a={12,25,7,65,32,55};
        sum(a);//求數組的和
        average(a);//求數組的平均值
        max(a);//求數組中的最大值
        min(a);//求數組中的最小值
        print(a);//打印數組

    }
    private static void print(int[] a) {
        // 打印數組
        System.out.print("數組打印結果爲:");
        for (int i = 0; i < a.length; i++) {
             System.out.print(a[i]+" ");
        }

    }
    private static void min(int[] a) {
        // 求最小值
        int min1=a[0];
        for (int i = 0; i < a.length; i++) {
            if(a[i]<min1){
                min1=a[i];
            }
        }
        System.out.println("數組最小值爲:"+min1);
    }
    private static void max(int[] a) {
        // 求數組中的最大值
        int max1=a[0];
        for (int i = 0; i < a.length; i++) {
            if(a[i]>max1){
                max1=a[i];
            }
            System.out.println();
        }
        System.out.println("數組中最大值爲:"+max1);
    }
    private static void average(int[] a) {
        // 數組求平均值(int)
        int y = 0,z=0;

        for (int i = 0; i < a.length; i++) {
            y+=a[i];
        }
        z=y/a.length;
        System.out.println("數組平均值爲:"+z);

    }
    private static void sum(int[] arr) {
        // 求和方法
        int x=0;
        for (int i = 0; i < arr.length; i++) {

            x+=arr[i];
        }
        System.out.println("數組求和結果爲:"+x);

    }

}

運行結果:
數組求和結果爲:196
數組平均值爲:32
數組中最大值爲:65
數組最小值爲:7
數組打印結果爲:12 25 7 65 32 55


作業二

  1. 給定兩個整型數組a和b,找出其共同元素。
    eg:int[] a={23,12,45,11};
    int[] b={99,33,12,45,67,11}
    要找出:12,45,11

代碼如下:

package homework2;

public class ElementFundDemo {

    public static void main(String[] args) {
        /**
         * 給定兩個整型數組a和b,找出其共同元素。
         * eg:int[] a={23,12,45,11};
         * int[] b={99,33,12,45,67,11};
         * 要找出:12,45,11
         */
        int[] a={23,12,45,11};
        int[] b={99,33,12,45,67,11};
        fund(a,b);
    }

    private static void fund(int[] a,int[] b) {
        /*
         *  查找相同元素
         *  並各自打印出對應的12,45,11
         */
        System.out.println("數組中相同的元素爲:");
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < b.length; j++) {
                if(a[i]==b[j]){
                    System.out.print(a[i]+"  ");
                }
            }
        }

    }

}

代碼運行結果:
數組中相同的元素爲:
12 45 11


作業三

  1. 在某歌唱比賽中,共有10位評委進行打分,在計算歌手得分時,去掉一個最高分,
    去掉一個最低分,然後剩餘的8位評委的分數進行平均,就是該選手的最終得分。
    10位評委對某歌手的打分分別是:78、60、85、96、77、66、60、82、88、85。
    計算該歌手的最終得分。

代碼如下:

package homework2;

import java.util.Scanner;

public class SingerScoreDemo {

    public static void main(String[] args) {
        /*在某歌唱比賽中,共有10位評委進行打分,在計算歌手得分時,去掉一個最高分,去掉一個最低分,
         * 然後剩餘的8位評委的分數進行平均,就是該選手的最終得分。
         * 10位評委對某歌手的打分分別是:78、60、85、96、77、66、60、82、88、85.計算該歌手的最終得分。
         */
         Scanner scan= new Scanner(System.in);
         float   a=0;
         float[] b=new float[10];
         int i=0;
         for ( i = 0; i < b.length; i++) {
             System.out.println("請分別輸入十位評委給出的分數:");
             a=scan.nextInt(); 
             b[i]=a;

        }
         for (int j = 0; j < b.length; j++) {
            System.out.print(b[j]+" ");
        }
        float score=finalScore(b); 
        System.out.println("該歌手最終成績爲:"+score);
        scan.close();
    }

    private static float finalScore(float[] b) {
        // 計算歌手最終成績
        float max=b[0];
        for (int i = 0; i < b.length; i++) {
            if(b[i]>max){
                max=b[i];
            }
        }
        float min=b[0];
        for (int i = 0; i < b.length; i++) {
            if(b[i]<min){
                min=b[i];
            }
        }
        System.out.println();
        System.out.print("去掉一個成績中的最大值:"+max+"去掉一個成績中的最小值:"+min);
        System.out.println();
        float average=0;
        float sum=0;
        for (int i = 0; i < b.length; i++) {
            sum+=b[i];
            average=(sum-max-min)/(b.length-2);
        }

        return average;
    }


}

運行結果:
請分別輸入十位評委給出的分數:
78
請分別輸入十位評委給出的分數:
60
請分別輸入十位評委給出的分數:
85
請分別輸入十位評委給出的分數:
96
請分別輸入十位評委給出的分數:
77
請分別輸入十位評委給出的分數:
66
請分別輸入十位評委給出的分數:
60
請分別輸入十位評委給出的分數:
82
請分別輸入十位評委給出的分數:
88
請分別輸入十位評委給出的分數:
85
78.0 60.0 85.0 96.0 77.0 66.0 60.0 82.0 88.0 85.0
去掉一個成績中的最大值:96.0去掉一個成績中的最小值:60.0
該歌手最終成績爲:77.625


作業四

  1. 兔子在出生兩個月後,就有繁殖能力,一對兔子每個月能生出一對小兔子來。
    初始有一對小兔子,假設所有兔子都不死,計算前12個月每個月的兔子對數。
    分析:
    第一個月:1對
    第二個月:1對
    第三個月:2對
    第四個月:3對
    第五個月:5對
    等等……..

代碼如下:

package homework2;

import java.util.Scanner;

public class RabbitDemo {

    public static void main(String[] args) {
        /**
         * 兔子在出生兩個月後,就有繁殖能力,一對兔子每個月能生出一對小兔子來。
         * 初始有一對小兔子,假設所有兔子都不死,計算前12個月每個月的兔子出生的對數。
         * 分析:
         * 第一個月:1對
         * 第二個月:1對
         * 第三個月:2對
         * 第四個月:3對
         * 第五個月:5對
         *......
         * 
         */
        int count=0;
        System.out.println("請輸入要計算兔子對數的月份:");
        Scanner scan=new Scanner(System.in);
        int month=scan.nextInt();
        for (int i = 1; i <= month; i++) {
            count=f(i);
            System.out.print("第"+i+"月出生的兔子數量:");
            System.out.println(count+"對 ");
        }
        System.out.println();
        scan.close();
    }

    private static int f(int x) {
        // 計算每個月出生的兔子的對數
        if (x == 1 || x == 2){
            return 1;
        }
        else if(x>2) {
            return f(x - 1) + f(x - 2);
        }
        return x;
    }

}

代碼運行結果:
請輸入要計算兔子對數的月份:
12
第1月出生的兔子數量:1對
第2月出生的兔子數量:1對
第3月出生的兔子數量:2對
第4月出生的兔子數量:3對
第5月出生的兔子數量:5對
第6月出生的兔子數量:8對
第7月出生的兔子數量:13對
第8月出生的兔子數量:21對
第9月出生的兔子數量:34對
第10月出生的兔子數量:55對
第11月出生的兔子數量:89對
第12月出生的兔子數量:144對

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章