新人求助编
谢谢(*°∀°)=3[此贴子已经被作者于2019-5-21 23:41编辑过]
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class Test0517 { class Student implements Comparable<Student> { String stuNo; String name; int score; public Student(String stuNo, String name, int score) { this.stuNo = stuNo; this.name = name; this.score = score; } @Override public String toString() { return stuNo +"\t" + name + "\t" + score; } @Override public int compareTo(Student o) { return score-o.score; } } private static final String ADD="0", DEL = "1", UPDATE="2", QUERY = "3", SORT = "4", SHOW="5", EXIT="-1"; public static void main(String[] args) { Map<String, Test0517.Student> students = new LinkedHashMap<>(); showMenu(); Scanner scanner = new Scanner(System.in); String commandTxt = scanner.nextLine(); while(!EXIT.equals(commandTxt)) { if(!Arrays.asList(EXIT, ADD, DEL, UPDATE, QUERY, SORT, SHOW).contains(commandTxt)) { System.out.println("命令有误:"+commandTxt); } else { executeMenu(commandTxt, scanner, students); } showMenu(); commandTxt = scanner.nextLine(); } scanner.close(); } private static void executeMenu(String commandTxt, Scanner scanner, Map<String, Student> students) { if(ADD.equals(commandTxt)) { String no = getInputByMsg("学号:", scanner); String name = getInputByMsg("姓名:", scanner); String score = getInputByMsg("成绩:", scanner); while(!score.matches("\\d+")) { String flag = getInputByMsg("成绩必须是整数,不纠正则不添加,纠正:1,不纠正任意键继续", scanner); if("1".equals(flag)) { score = getInputByMsg("成绩:", scanner); } else { return; } } students.put(no, new Test0517().new Student(no, name, Integer.parseInt(score))); } else if(DEL.equals(commandTxt)) { String no = getInputByMsg("请输入要删除的学号:", scanner); if(!students.keySet().contains(no)) { System.out.println("学号" + no + "不存在,无删除操作"); } else { students.remove(no); } } else if(UPDATE.equals(commandTxt)) { String no = getInputByMsg("请输入要修改的学号:", scanner); if(!students.keySet().contains(no)) { System.out.println("学号" + no + "不存在,无删除操作"); } else { String name = getInputByMsg("姓名:", scanner); String score = getInputByMsg("成绩:", scanner); while(!score.matches("\\d+")) { String flag = getInputByMsg("成绩必须是整数,不纠正则不修改,纠正:1,不纠正任意键继续", scanner); if("1".equals(flag)) { score = getInputByMsg("成绩:", scanner); } else { return; } } students.put(no, new Test0517().new Student(no, name, Integer.parseInt(score))); } } else if(QUERY.equals(commandTxt)) { String no = getInputByMsg("请输入要查询的学号:", scanner); if(!students.keySet().contains(no)) { System.out.println("学号" + no + "不存在"); } else { System.out.println(students.get(no)); } } else if(SORT.equals(commandTxt)) { List<Student> tmpStus = new ArrayList<Student>(students.values()); Collections.sort(tmpStus); students.clear(); for(Student stu : tmpStus) students.put(stu.stuNo, stu); } else if(SHOW.equals(commandTxt)) { System.out.println("学号\t姓名\t成绩"); for(Student stu : students.values()) { System.out.println(stu); } } } private static String getInputByMsg(String msg, Scanner scanner) { System.out.print(msg); return scanner.nextLine(); } private static void showMenu() { System.out.println("====================================="); System.out.println("增加:0、删除:1、修改:2、查询:3、排序:4、输出:5"); System.out.println("退出:-1"); System.out.print("请输入指令:"); } }