評委打分系統

題目:在一場比賽中,有5位評委爲同一個運動員打分,根據大賽規則,去掉最高分,去掉最低分,計算得平均分。爲選手的最終得分。

1、求出選手的最終得分

2、請找出打出最高分、最低分的裁判員分別是那兩位

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 題目:跳水比賽,
 * 1、有5裁判,去掉最高分,去掉最低分,求平均得分
 * 2、把打最高分和最低分的評委找出來
 *
 */
public class Test4 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Judge jg = new Judge();
		//輸出平均得分
		System.out.println("平均得分是:"+jg.getLastFen());
		//打出最高、最低分的評委
		System.out.println("打出最高分的評委是第"+(jg.getMax()+1)+"位評委");
		System.out.println("打出最低分的評委是第"+(jg.getMin()+1)+"位評委");
	}
}

class Judge{
	//定義數組大小
	int size=5;
	float [] arr=null;
	//構造函數
	public Judge(){
		arr = new float[size];
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		// 1.將所有裁判的打分接收進數組

		try {
			for (int i = 0; i < arr.length; i++) {
				System.out.println("請輸入第" + (i + 1) + "個裁判的打分:");
				arr[i] = Float.parseFloat(br.readLine());
			}
		} catch (NumberFormatException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("您的輸入有誤!");
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("關閉出現異常");
			}
		}
	}
	
	//2.找到最高分
	public int getMax(){
		int maxIndex=0;
		float max=arr[0];
		for(int i=0;i<arr.length;i++){
			if(arr[i]>max){
				max=arr[i];
				maxIndex=i;
			}
		}
//		max=arr[maxIndex];
		return maxIndex;
	}
	//3.找到最低分
	public int getMin(){
		int minIndex=0;
		float min=arr[0];
		for(int i=0;i<arr.length;i++){
			if(arr[i]<min){
				min=arr[i];
				minIndex=i;
			}
		}
		return minIndex;
	}
	//4.求出最終得分
	public float getLastFen(){
		float lastFen =0;
		float all=0;
		int maxIndex=this.getMax();
		int minIndex=this.getMin();
		for(int i=0;i<arr.length;i++){
			if(i!=maxIndex||i!=minIndex){
				all+=arr[i];
			}
		}
		return (all/(arr.length-2));
	}
}


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