[双体系]练习1:控制台输入练习

关键问题在于:
及时清除缓冲区换行符

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public class Application {
//    static Scanner sc = new Scanner(System.in, StandardCharsets.UTF_8);
    static Scanner sc = new Scanner(System.in);
    public static void setJugesNumber(RuanYue ruanYue){
        while (true){
            System.out.println("请输入评委数量:");
            try{
                ruanYue.setJudgesNumbber(sc.nextInt());
                sc.nextLine(); // 清除输入缓冲区中的换行符
                if( ruanYue.getJudgesNumbber()>10){
                    System.out.println("最多请10个评委,多了发不起工资");
                    continue;
                }
                if( ruanYue.getJudgesNumbber()<0){
                    System.out.println("华强买瓜,你是华强吗?");
                    continue;
                }
                break;
            }catch (Exception e){
                System.out.println("输入错误,请输入数字");
                sc.nextLine(); // 清除输入缓冲区中的换行符
            }
        }
    }
    public static void main(String[] args) {
        System.out.println("Welcome to ShuangTiRuanYue System");
        // 准备对象
        RuanYue ruanYue = new RuanYue();
        // 设置评委数量
        setJugesNumber(ruanYue);
        // 选手列表
        List<XuanShou> xuanShouList = new ArrayList<XuanShou>();
        // 变量设定


        boolean isRun = true;
        while (isRun){
            int needTo;
            // 让用户输入
            while (true) {
            // 展示选项
            System.out.println("""
                    \n---------------
                    1.创建选手信息
                    2.对当前选手评分
                    3.查询所有选手评分信息
                    4.删除选手
                    5.退出系统
                    (软约评分)请输入指令:
                    """);
            try {
                needTo = sc.nextInt();
                sc.nextLine();
                break;
            } catch (Exception e) {
                System.out.println("输入错误,请输入数字");
                sc.nextLine();
            }
        }
            // 根据用户输入做出决定
            switch (needTo){
                case 1:
                    createUserInfo(xuanShouList); // 创建选手信息
                    break;
                case 2:
                    userGrade(xuanShouList,ruanYue); // 评分
                    break;
                case 3:
                    selectAllUserGrade(xuanShouList);//查询所有选手评分信息
                    break;
                case 4:
                    deleteUser(xuanShouList);// 删除选手
                    break;
                case 5:
                    isRun = false;
                    break;
                default:
                    System.out.println("无效的输入,输入范围1-5");
            }
        }


    }

    /**
     * 删除选手
     * @param xuanShouList 选手列表
     */
    private static void deleteUser(List<XuanShou> xuanShouList)
    {
        String name;
        while (true){
            System.out.println("请输入要删除选手的姓名(输入0则会退出)");
            name = sc.nextLine();
            if (Objects.equals(name, "0")){
                return;
            }
            if (name.isEmpty()){
                continue;// 空输入,重来吧
            }
            name = name.trim();// 去掉首尾多余空格
            break;
        }
        // 在列表中查找这个用户,然后进行删除
        boolean found = false;
        // 用遍历的方法,找到要删除选手的索引,然后再根据索引删除列表元素
        for (int i = 0; i < xuanShouList.size(); i++) {
            if (xuanShouList.get(i).getName().equals(name)) {
                xuanShouList.remove(i);
                found = true;
                break;
            }
        }

        if (found) {
            System.out.println("选手 " + name + " 已成功删除。");
        } else {
            System.out.println("未找到名为 " + name + " 的选手。");
        }

    }

    /**
     * 查询所有选手的成绩
     * @param xuanShouList 选手列表
     */
    private static void selectAllUserGrade(List<XuanShou> xuanShouList) {
        if (xuanShouList.isEmpty()){
            System.out.println("暂无选手,请先添加选手");
            return;
        }
        for(XuanShou xuanShou : xuanShouList){
//            if (xuanShou.getZongfen()!=-1){
//                System.out.printf("选手 %s 获得 %f 分\n".formatted(xuanShou.getName(), xuanShou.getZongfen()));
//            }else {
//                System.out.printf("选手 %s 未评分\n".formatted(xuanShou.getName()));
//            }
            System.out.printf("""
                    \n
                    选手:%s
                    宣言:%s
                    分数:%s
                    """.formatted(xuanShou.getName(),xuanShou.getXuanyan(),xuanShou.getZongfen()==-1?"未评分": String.format("%.2f",xuanShou.getZongfen())));
        }
    }

    private static void userGrade(List<XuanShou> xuanShouList,RuanYue ruanYue) {
        if (xuanShouList.isEmpty()){
            System.out.println("暂无选手,请先添加选手");
            return;
        }
        while (true){
            int i=0;
            for (XuanShou xuanShou : xuanShouList) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(i+1);
                stringBuilder.append(" ");
                stringBuilder.append(xuanShou.getName());
                stringBuilder.append(" ");
                stringBuilder.append(xuanShou.getZongfen()==-1?"未评分":xuanShou.getZongfen());
                System.out.println(stringBuilder);
                i++;
            }
            System.out.println("请输入选手序号以评分(输入0则退出)");
            int xs;
            try{
                 xs = sc.nextInt();
                 if (xs==0){
                     break;
                 }
                 if (xs>xuanShouList.size()){
                     System.out.println("该序号无选手");
                     continue;
                 }
                 if(xuanShouList.get(xs-1).getZongfen()!=-1){
                     System.out.println("该选手已评分,无需再次评分");
                     continue;
                 }
                pingfen(ruanYue,xuanShouList.get(xs-1));
                break;
            }catch (Exception e){
                System.out.println("选手序号错误,请重新输出");
                sc.next();
            }



        }
    }

    /**
     * 评分
     * @param ruanYue 软约对象 主要是为了获取评委数量
     * @param xuanShou 选手。被评分的选手对象
     */
    private static void pingfen(RuanYue ruanYue, XuanShou xuanShou) {
        List<Double> pingfenList = new ArrayList<Double>();
        // 获取评委数量
        for (int i=1;i<=ruanYue.getJudgesNumbber();i++){
            while (true){
                System.out.println("请输入第"+i+"位评委的评分:");
                try{
                    double temp =  sc.nextDouble();
                    if(temp>100 || temp<0){
                        System.out.println("分数范围0-100,请重新输出");
                        continue;
                    }
                    pingfenList.add(temp);
                    break;
                }catch (Exception e){
                    System.out.println("请输入分数,懂?");
                    sc.next(); // 清除输入缓冲区
                }
            }
        }
        // 根据pingfenList 求出平均分
        double averageScore = calculateAverageScore(pingfenList);
        System.out.println(xuanShou.getName()+"平均分是:" + String.format("%.2f",averageScore));
        xuanShou.setZongfen(averageScore);
    }

    /**
     * 计算平均分
     * @param scores 分数列表
     * @return 平均分
     */
    private static double calculateAverageScore(List<Double> scores) {
        if (scores.isEmpty()) {
            return 0.0;
        }
        double sum = 0.0;
        for (double score : scores) {
            sum += score;
        }
        return sum / scores.size();
    }

    /**
     * 创建选手
     * @param xuanShouList 选手列表
     */
    private static void createUserInfo(List<XuanShou> xuanShouList) {
        // 设置变量
        String name;
        String xuanyan;

        while (true){
            System.out.println("请输入姓名");
            name = sc.nextLine();
            if (name.isEmpty()){
                continue;// 空输入,重来吧
            }
            name = name.trim();// 去掉首尾多余空格
            break;
        }
        while (true){
            System.out.println("请输入宣言");
            xuanyan = sc.nextLine();
            if (xuanyan.isEmpty()){
                continue;
            }
            xuanyan = xuanyan.trim();
            break;
        }
        // 存储数据
        XuanShou xuanShou = new XuanShou(name, xuanyan,-1);
        xuanShouList.add(xuanShou);
    }


}


效果展示:

设置评委数量

image

新增用户并查询用户

image

无用户时查询用户

image

评分

image

已经评分过的不能重复评分

image

删除选手

没有这个选手的时候

image

有这个选手的时候

image

优化

  1. 修复 评委0的问题
  2. 更改删除模式(根据姓名删除)为(根据ID删除)
  3. 修复 序号<0的问题
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public class Application {
//    static Scanner sc = new Scanner(System.in, StandardCharsets.UTF_8);
    static Scanner sc = new Scanner(System.in);
    /**
     * 设置评委数量
     * @param ruanYue 软约
     */
    public static void setJugesNumber(RuanYue ruanYue){
        while (true){
            System.out.println("请输入评委数量:");
            try{
                ruanYue.setJudgesNumbber(sc.nextInt());
                sc.nextLine(); // 清除输入缓冲区中的换行符
                if( ruanYue.getJudgesNumbber()>10){
                    System.out.println("最多请10个评委,多了发不起工资");
                    continue;
                }
                if( ruanYue.getJudgesNumbber()<0){
                    System.out.println("华强买瓜,你是华强吗?");
                    continue;
                }
                if(ruanYue.getJudgesNumbber()==0){
                    System.out.println("这比赛,不办也罢!");
                    continue;
                }
                break;
            }catch (Exception e){
                System.out.println("输入错误,请输入数字");
                sc.nextLine(); // 清除输入缓冲区中的换行符
            }
        }
    }
    public static void main(String[] args) {
        System.out.println("Welcome to ShuangTiRuanYue System");
        // 准备对象
        RuanYue ruanYue = new RuanYue();
        // 设置评委数量
        setJugesNumber(ruanYue);
        // 选手列表
        List<XuanShou> xuanShouList = new ArrayList<XuanShou>();
        // 变量设定


        boolean isRun = true;
        while (isRun){
            int needTo;
            // 让用户输入
            while (true) {
            // 展示选项
            System.out.println("""
                    \n---------------
                    1.创建选手信息
                    2.对当前选手评分
                    3.查询所有选手评分信息
                    4.删除选手
                    5.退出系统
                    (软约评分)请输入指令:
                    """);
            try {
                needTo = sc.nextInt();
                sc.nextLine();
                break;
            } catch (Exception e) {
                System.out.println("输入错误,请输入数字");
                sc.nextLine();
            }
        }
            // 根据用户输入做出决定
            switch (needTo){
                case 1:
                    createUserInfo(xuanShouList); // 创建选手信息
                    break;
                case 2:
                    userGrade(xuanShouList,ruanYue); // 评分
                    break;
                case 3:
                    selectAllUserGrade(xuanShouList);//查询所有选手评分信息
                    break;
                case 4:
                    deleteUser(xuanShouList);// 删除选手
                    break;
                case 5:
                    isRun = false;
                    break;
                default:
                    System.out.println("无效的输入,输入范围1-5");
            }
        }


    }

    /**
     * 删除选手
     * @param xuanShouList 选手列表
     */
    private static void deleteUser(List<XuanShou> xuanShouList)
    {
        // 下面的方法是根据名字删除
//        String name;
//        while (true){
//            System.out.println("请输入要删除选手的姓名(输入0则会退出)");
//            name = sc.nextLine();
//            if (Objects.equals(name, "0")){
//                return;
//            }
//            if (name.isEmpty()){
//                continue;// 空输入,重来吧
//            }
//            name = name.trim();// 去掉首尾多余空格
//            break;
//        }
//        // 在列表中查找这个用户,然后进行删除
//        boolean found = false;
//        // 用遍历的方法,找到要删除选手的索引,然后再根据索引删除列表元素
//        for (int i = 0; i < xuanShouList.size(); i++) {
//            if (xuanShouList.get(i).getName().equals(name)) {
//                xuanShouList.remove(i);
//                found = true;
//                break;
//            }
//        }
//
//        if (found) {
//            System.out.println("选手 " + name + " 已成功删除。");
//        } else {
//            System.out.println("未找到名为 " + name + " 的选手。");
//        }
        // 下面的方法是根据需要删除
        if (xuanShouList.isEmpty()){
            System.out.println("暂无选手,请先添加选手");
            return;
        }
        while (true){
            int i=0;
            for (XuanShou xuanShou : xuanShouList) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(i+1);
                stringBuilder.append(" ");
                stringBuilder.append(xuanShou.getName());
                stringBuilder.append(" ");
                stringBuilder.append(xuanShou.getZongfen()==-1?"未评分":xuanShou.getZongfen());
                System.out.println(stringBuilder);
                i++;
            }
            System.out.println("请输入选手序号以删除(输入0则退出)");
            int xs;
            try{
                xs = sc.nextInt();
                if (xs==0){
                    break;
                }
                if (xs>xuanShouList.size()){
                    System.out.println("该序号无选手");
                    continue;
                }
                if(xs<0){
                    System.out.println("非正确序号");
                    continue;
                }
                xuanShouList.remove(xs-1);
                System.out.println("删除成功!世上没有后悔药。");
                break;
            }catch (Exception e){
                System.out.println("选手序号错误,请重新输入");
                sc.next();
            }



        }


    }

    /**
     * 查询所有选手的成绩
     * @param xuanShouList 选手列表
     */
    private static void selectAllUserGrade(List<XuanShou> xuanShouList) {
        if (xuanShouList.isEmpty()){
            System.out.println("暂无选手,请先添加选手");
            return;
        }
        for(XuanShou xuanShou : xuanShouList){
//            if (xuanShou.getZongfen()!=-1){
//                System.out.printf("选手 %s 获得 %f 分\n".formatted(xuanShou.getName(), xuanShou.getZongfen()));
//            }else {
//                System.out.printf("选手 %s 未评分\n".formatted(xuanShou.getName()));
//            }
            System.out.printf("""
                    \n
                    选手:%s
                    宣言:%s
                    分数:%s
                    """.formatted(xuanShou.getName(),xuanShou.getXuanyan(),xuanShou.getZongfen()==-1?"未评分": String.format("%.2f",xuanShou.getZongfen())));
        }
    }

    private static void userGrade(List<XuanShou> xuanShouList,RuanYue ruanYue) {
        if (xuanShouList.isEmpty()){
            System.out.println("暂无选手,请先添加选手");
            return;
        }
        while (true){
            int i=0;
            for (XuanShou xuanShou : xuanShouList) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(i+1);
                stringBuilder.append(" ");
                stringBuilder.append(xuanShou.getName());
                stringBuilder.append(" ");
                stringBuilder.append(xuanShou.getZongfen()==-1?"未评分":xuanShou.getZongfen());
                System.out.println(stringBuilder);
                i++;
            }
            System.out.println("请输入选手序号以评分(输入0则退出)");
            int xs;
            try{
                 xs = sc.nextInt();
                 if (xs==0){
                     break;
                 }
                 if (xs>xuanShouList.size()){
                     System.out.println("该序号无选手");
                     continue;
                 }
                 if(xuanShouList.get(xs-1).getZongfen()!=-1){
                     System.out.println("该选手已评分,无需再次评分");
                     continue;
                 }
                if(xs<0){
                    System.out.println("非正确序号");
                    continue;
                }
                pingfen(ruanYue,xuanShouList.get(xs-1));
                break;
            }catch (Exception e){
                System.out.println("选手序号错误,请重新输入");
                sc.next();
            }



        }
    }

    /**
     * 评分
     * @param ruanYue 软约对象 主要是为了获取评委数量
     * @param xuanShou 选手。被评分的选手对象
     */
    private static void pingfen(RuanYue ruanYue, XuanShou xuanShou) {
        List<Double> pingfenList = new ArrayList<Double>();
        // 获取评委数量
        for (int i=1;i<=ruanYue.getJudgesNumbber();i++){
            while (true){
                System.out.println("请输入第"+i+"位评委的评分:");
                try{
                    double temp =  sc.nextDouble();
                    if(temp>100 || temp<0){
                        System.out.println("分数范围0-100,请重新输出");
                        continue;
                    }
                    pingfenList.add(temp);
                    break;
                }catch (Exception e){
                    System.out.println("请输入分数,懂?");
                    sc.next(); // 清除输入缓冲区
                }
            }
        }
        // 根据pingfenList 求出平均分
        double averageScore = calculateAverageScore(pingfenList);
        System.out.println(xuanShou.getName()+"平均分是:" + String.format("%.2f",averageScore));
        xuanShou.setZongfen(averageScore);
    }

    /**
     * 计算平均分
     * @param scores 分数列表
     * @return 平均分
     */
    private static double calculateAverageScore(List<Double> scores) {
        if (scores.isEmpty()) {
            return 0.0;
        }
        double sum = 0.0;
        for (double score : scores) {
            sum += score;
        }
        return sum / scores.size();
    }

    /**
     * 创建选手
     * @param xuanShouList 选手列表
     */
    private static void createUserInfo(List<XuanShou> xuanShouList) {
        // 设置变量
        String name;
        String xuanyan;

        while (true){
            System.out.println("请输入姓名");
            name = sc.nextLine();
            if (name.isEmpty()){
                continue;// 空输入,重来吧
            }
            name = name.trim();// 去掉首尾多余空格
            break;
        }
        while (true){
            System.out.println("请输入宣言");
            xuanyan = sc.nextLine();
            if (xuanyan.isEmpty()){
                continue;
            }
            xuanyan = xuanyan.trim();
            break;
        }
        // 存储数据
        XuanShou xuanShou = new XuanShou(name, xuanyan,-1);
        xuanShouList.add(xuanShou);
    }


}

posted @ 2024-10-15 15:11  萌狼蓝天  阅读(23)  评论(0编辑  收藏  举报