JAVA基礎--歌手打分功能實現

來源:互聯網
上載者:User

標籤:java基礎   選擇   system.in   tin   void   最小值   ati   bsp   return   

問題:在歌唱比賽中,共有10位評委進行打分,在計算歌手得分時,去掉一個最高分,去掉一個最低分,然後剩餘的8位評委的分數進行平均,就是該選手的最終得分。輸入每個評委的評分,求某選手的得分。

分析:該題的關鍵在於,當最高分和最低分多次出現時,如何處理能夠使最高分和最低分只去掉一次。可以設定布爾值,讓去掉操作只執行一次。

測試方法:

 1 public static void test(){ 2         Scanner sc = new Scanner(System.in); 3         System.out.println("表演完畢,請各評委打分:"); 4         int[] a = new int[10]; 5         for(int i=0;i<10;i++){ 6             int score = sc.nextInt(); 7             a[i] = score; 8         } 9         System.out.println("10位評委的評分分別是:");10         show(a);11         int[] b = getScore(a);12         System.out.println("去掉一個最高分,一個最低分後:");13         show(b);14         getAvg(b);15         16     }

列印方法:

//遍曆數組    public static void show(int[] a){        for (int i = 0; i < a.length; i++) {            System.out.print(a[i]+" ");        }        System.out.println();    }

擷取最大元素:

//求最大值    public static int getMax(int[] a){        int max = a[0];        for (int i = 0; i < a.length; i++) {            if(a[i]>max){                max = a[i];            }        }        return max;        //System.out.println("該數組最大元素為:"+max);    }

擷取最小元素:

//求最小值    public static int getMin(int[] a){        int min = a[0];        for (int i = 0; i < a.length; i++) {            if(a[i]<min){                min = a[i];            }        }        return min;        //System.out.println("該數組最小元素為:"+min);    }

 得到新的得分數組:

//評分方法    //參數:得分數組    //傳回值:去掉最高分和最低分後的數組    public static int[] getScore(int[] a){        int max = getMax(a);        int min = getMin(a);        int l = a.length-2;        int[] b = new int[l];        int count = 0;//計數器        boolean flag1 = true;        boolean flag2 = true;        for(int i=0;i<a.length;i++){//將原數組的值,選擇性的賦給新數組            if(a[i]==max&&flag1==true){                flag1 = false;                continue;            }            if(a[i]==min&&flag2==true){                flag2 = false;                continue;            }            if(count<l){                b[count] = a[i];                count++;            }                    }                return b;            }

得到最終平均得分:

//求平均數    public static void getAvg(int[] a){        int sum = 0;        for (int i = 0; i < a.length; i++) {            sum+=a[i];        }        int avg = sum/a.length;        System.out.println("該選手最終得分:"+avg);    }

 

JAVA基礎--歌手打分功能實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.