一个评委评分命令行程序,用于某舞蹈大赛选手评分,具备如下功能:
(1)用户能设定评委人数和选手人数;
(2)某选手完成表演后,能输入该选手的姓名,每个评委(评委用数字编号表示,0表示评委1,其他类推)的评分(区间[0,10]上);
(3)选手最终得分计算规则:去掉1个最高分和1个最低分,求其余的平均分(保留小数点后两位有效数字)。
(4)比赛完毕,程序不会自动退出,除非用户选择退出;
(5)比赛完毕后,只要程序没有退出,用户能随时选择升序或降序输出选手得分信息,包括“排名、选手顺序号、选手姓名、评分1、评分2、…评分N、最终得分”。
public class Menu {
/**
* @Description 主函数
* */
public static void main(String[] args){
System.out.println("欢迎使用评委评分系统");
boolean flag = true;
while (flag) {
int menuLength = showMenu();
int menuNumber = getMenuNum(menuLength);
// 输入序号超出范围
while(menuNumber == -1)
menuNumber = getMenuNum(showMenu());
// 输入2退出程序
if(menuNumber == 2) flag = false;
chooseEvent(menuNumber);
}
}
/**
* @return 菜单长度
* @Description 评委评分系统事务菜单
* */
public static int showMenu(){
System.out.println("\n=============");
System.out.println("0 -- 信息录入");
System.out.println("1 -- 输出排名");
System.out.println("2 -- 退出程序");
System.out.println("=============");
return 3;
}
/**
* @param menuLength 菜单长度
* @return 事务对应序号
* @Description 用户选择事务
* */
public static int getMenuNum(int menuLength){
int menuNum = CatchError.catchInt("输入事务序号:","\t[警告]输入类型错误,请重新输入!");
if(menuNum >=0 && menuNum <= menuLength-1)
return menuNum;
else{
System.out.println("\t[警告]输入范围错误,请重新输入!");
return -1;
}
}
/**
* @param chooseIndex 事务对应序号
* @Description 评分系统主程序,选择运行用户所选择的事务
* */
public static void chooseEvent(int chooseIndex) {
switch (chooseIndex) {
case 0:
gameScoreEntry();
System.out.println("\t[提示]比赛计分结束!\n\n");
break;
case 1:
gameScoreSort();
System.out.println("\t[提示]排名输出完毕!\n\n");
break;
case 2:
System.out.println("\t[提示]评分系统结束!\n\n");
break;
}
}
/**
* @Description 选手姓名信息录入 以及 评委打分信息录入
* */
public static void gameScoreEntry(){
System.out.println("\n----- 比赛选手信息录入 -----");
PlayerTable.setPlayerNum();
PlayerTable.setPlayerNames();
System.out.println("\n----- 评委打分信息录入 -----");
JudgeTable.setJudgeNum();
JudgeTable.setJudgeNames();
ScoreTable.setScoreEntryTable();
}
/**
* @Description 比赛评分结果排序展示
* */
public static void gameScoreSort(){
if(ScoreTable.scoreEntryTable == null) {
System.out.println("\t[警告]比赛信息尚未录入,请先录入相关信息!");
gameScoreEntry();
}
System.out.println("请输入排序方式 -- 升序:1,降序:0");
int sortPattern = CatchError.catchInt("","\t[警告]输入类型错误,请重新输入!");
boolean flag = true;
while (flag) {
while(sortPattern != 0 && sortPattern != 1){
System.out.println("\t[警告]输入错误,应输入0或1,请重新输入!");
sortPattern = CatchError.catchInt("","\t[警告]输入类型错误,请重新输入!");
}
flag = false;
}
ScoreTable.sortScoreFinalTable(sortPattern);
ScoreTable.showScoreFinalTable(sortPattern);
}
}
public class JudgeTable {
/** 裁判人数 */
public static int judgeNum;
/** 裁判序号 */
public static int[] judgeIndex;
/**
* @Description 设置评委人数
* */
public static void setJudgeNum(){
judgeNum = CatchError.catchInt("输入评委人数:","\t[警告]输入类型错误,请重新输入!");
while(judgeNum < 3){
System.out.println("\t[警告]参赛评委至少3人以上,请重新输入!");
judgeNum = CatchError.catchInt("输入评委人数:","\t[警告]输入类型错误,请重新输入!");
}
}
/**
* @Description 设置评委序号
* */
public static void setJudgeNames(){
judgeIndex = new int[judgeNum];
for(int i = 0; i < judgeNum; i++)
judgeIndex[i] = i;
}
}
public class PlayerTable {
/** 选手人数 */
public static int playerNum;
/** 选手姓名 */
public static String[] playerName;
/**
* @Description 设置选手人数
* */
public static void setPlayerNum(){
playerNum = CatchError.catchInt("输入选手人数:","\t[警告]输入类型错误,请重新输入!");
while(playerNum < 2){
System.out.println("\t[警告]参赛选手至少2人以上,请重新输入!");
playerNum = CatchError.catchInt("输入选手人数:","\t[警告]输入类型错误,请重新输入!");
}
}
/**
* @Description 设置选手姓名
* */
public static void setPlayerNames(){
playerName = new String[playerNum];
for(int i = 0; i < playerNum; i++) {
playerName[i] = CatchError.catchString("\t输入序号"+i+"的选手名字:","\t[警告]输入类型错误,请重新输入该选手的名字:");
}
}
}
import java.util.Arrays;
public class ScoreTable {
/** 初始登分表 */
public static double[][] scoreEntryTable;
/** 最终登分表 */
public static double[][] scoreFinalTable;
/**
* @Description 设置输入打分表
* */
public static void setScoreEntryTable(){
scoreEntryTable = new double[PlayerTable.playerNum][JudgeTable.judgeNum];
for(int i = 0; i < PlayerTable.playerNum; ++i){
System.out.println("请录入"+i+"号选手 "+ PlayerTable.playerName[i] + " 的打分表:");
for(int j = 0; j < JudgeTable.judgeNum; ++j){
while (true) {
System.out.print("\t评委 "+j+" 评分:");
scoreEntryTable[i][j] = CatchError.catchDouble("", "\t[警告]输入类型错误,请重新输入该选手的打分:");
if (scoreEntryTable[i][j] >= 0.0 && scoreEntryTable[i][j] <= 10.0) break;
System.out.println("\t[警告]输入评分应在0-10之间,请重新输入!");
}
}
}
}
/**
* @Description 设置最终打分表
* */
private static void setScoreFinalTable(){
scoreFinalTable = new double[PlayerTable.playerNum][JudgeTable.judgeNum+2];
for(int i = 0; i < PlayerTable.playerNum; ++i){
// 选手序号
scoreFinalTable[i][0] = i;
// 选手平均成绩
scoreFinalTable[i][1] = calculateAverageScore(i);
// 评委评分
if (JudgeTable.judgeNum >= 0) System.arraycopy(scoreEntryTable[i], 0, scoreFinalTable[i], 2, JudgeTable.judgeNum);
}
}
/**
* @param playerSerial 选手序号
* @Description 计算序号playerSerial选手的最终成绩
* */
private static double calculateAverageScore(int playerSerial) {
double maxScore = getArrayMax(scoreEntryTable[playerSerial]);
double minScore = getArrayMin(scoreEntryTable[playerSerial]);
double sumScore = Arrays.stream(scoreEntryTable[playerSerial]).sum();
return (sumScore - maxScore - minScore) / (double)(scoreEntryTable[playerSerial].length - 2);
}
/**
* @param sortPattern 排序模式
* @Description 对选手按最终成绩进行排序,排序模式由用户定义
* */
public static void sortScoreFinalTable(int sortPattern){
setScoreFinalTable();
sortArray(scoreFinalTable, PlayerTable.playerNum, sortPattern);
}
/**
* @param sortPattern 排序模式
* @Description 输出最终的排序结果
* */
public static void showScoreFinalTable(int sortPattern) {
System.out.print("排名");
System.out.print("\t选手顺序号");
System.out.print("\t选手姓名");
for (int k = 0; k < PlayerTable.playerNum; ++k) {
System.out.print("\t评分" + k);
}
System.out.println("\t最终得分");
for (int i = 0; i < PlayerTable.playerNum; ++i) {
// 排名
if (sortPattern == 1)
System.out.print(i + 1);
else
System.out.print(PlayerTable.playerNum - i);
// 选手顺序号
System.out.print("\t\t" + (int) scoreFinalTable[i][0]);
// 选手姓名
System.out.print("\t\t" + PlayerTable.playerName[(int) scoreFinalTable[i][0]]);
// 评分
for (int k = 0; k < PlayerTable.playerNum; ++k) {
System.out.print("\t\t" + scoreEntryTable[i][k]);
}
// 平均分
System.out.printf("\t\t%.1f", scoreFinalTable[i][1]);
System.out.println();
}
}
/**
* @param arr 浮点型数组
* @return 最大值
* @Description 获取数组最大值
* */
private static double getArrayMax(double[] arr) {
double max = arr[0];
for (int i = 1; i < arr.length; i++) if (max < arr[i]) max = arr[i];
return max;
}
/**
* @param arr 浮点型数组
* @return 最小值
* @Description 获取数组最小值
* */
private static double getArrayMin(double[] arr) {
double min = arr[0];
for (int i = 1; i < arr.length; i++) if (min > arr[i]) min = arr[i];
return min;
}
/**
* @param array 浮点型数组
* @param height 数组长度
* @param sortPattern 排序模式
* @Description 二维数组根据第一列元素大小排序,排序模式由用户定义
* */
private static void sortArray(double[][] array, int height, int sortPattern) {
for (int i = 0; i < height; i++) array[i][1] = array[i][1] * 10;
if (sortPattern == 0) Arrays.sort(array, (e1, e2) -> (int) (e1[1] - e2[1]));
else Arrays.sort(array, (e2, e1) -> (int) (e1[1] - e2[1]));
for (int i = 0; i < height; i++) array[i][1] = array[i][1] / 10;
}
}
import java.util.Scanner;
public class CatchError {
/**
* @param promptMessage 输入提示信息
* @param errMessage 错误提示信息
* @return 输入的整型
* @Description 对输入整型进行判断
* */
public static int catchInt(String promptMessage, String errMessage) {
Scanner sc = new Scanner(System.in);
System.out.print(promptMessage);
while(true) {
try {
return sc.nextInt();
} catch (Exception var3) {
System.out.println(errMessage);
sc.nextLine();
}
}
}
/**
* @param promptMessage 输入提示信息
* @param errMessage 错误提示信息
* @return 输入的字符串
* @Description 对输入字符串进行判断
* */
public static String catchString(String promptMessage, String errMessage) {
Scanner sc = new Scanner(System.in);
System.out.print(promptMessage);
while(true) {
try {
return sc.next();
} catch (Exception var3) {
System.out.println(errMessage);
sc.nextLine();
}
}
}
/**
* @param promptMessage 输入提示信息
* @param errMessage 错误提示信息
* @return 输入的浮点型
* @Description 对输入浮点型进行判断
* */
public static double catchDouble(String promptMessage, String errMessage) {
Scanner sc = new Scanner(System.in);
System.out.print(promptMessage);
while(true) {
try {
return sc.nextDouble();
} catch (Exception var4) {
System.out.print(errMessage);
sc.nextLine();
}
}
}
}