package 作业; import java.util.Scanner; //控制输入 import java.awt.AWTException; //用来清空控制台的 import java.awt.Robot; //用来清空控制台的 import java.awt.event.InputEvent; //用来清空控制台的 import java.awt.event.KeyEvent; //用来清空控制台的 class Student { private String stunumber = "00000000"; private String name = "***"; private int age = 0; private boolean sex; // 男生为 false,女生为 true。 private double score = 0; Student(String stunumber, String name, int age, boolean sex, double score) { this.stunumber = stunumber; this.name = name; this.age = age; this.sex = sex; this.score = score; } Student() { } public boolean isSex() { return sex; } public String getStunumber() { return stunumber; } public void setStunumber(String stunumber) { this.stunumber = stunumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void getSex() { if (this.sex == true) System.out.printf("女生"); else System.out.printf("男生"); } public void setSex(boolean sex) { this.sex = sex; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } } public class 作业 { public static void main(String[] args) throws AWTException { Student[] stu = new Student[10]; stu[0] = new Student("20170001", "王大", 18, true, 88); stu[1] = new Student("20170002", "李二", 17, false, 68); stu[2] = new Student("20170003", "张三", 16, true, 66); stu[3] = new Student("20170004", "李四", 16, false, 93); stu[4] = new Student("20170005", "王五", 17, true, 83); stu[5] = new Student(); stu[6] = new Student(); stu[7] = new Student(); stu[8] = new Student(); stu[9] = new Student(); int key = 0; while (key != 6) { System.out.println(" 1、 遍历输出学生信息;"); System.out.println(" 2、 新学生信息录入;"); System.out.println(" 3、 删除学生信息;"); System.out.println(" 4、 修改学生信息;"); System.out.println(" 5、 查询学生信息;"); System.out.println(" 6、 退出系统!;"); System.out.print("请输入:"); @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); key = scanner.nextInt(); // boolean a = true ; switch (key) { case 1: clear(); for (int i = 0; i < 10; i++) { if (stu[i].getName() != "***") ShowStudent(stu[i]); } System.out.println("输入任何数字返回主菜单"); System.out.println("6.退出系统"); System.out.print("请输入:"); int m1; Scanner scanner10 = new Scanner(System.in); m1 = scanner10.nextInt(); if (m1 != 6) { clear(); key = 0; break; } else if (m1 == 6) { key = 6; break; } break; case 2: clear(); int j; for (j = 0; j < 10;) { if (stu[j].getStunumber().equals("00000000")) break; else j = j + 1; } // System.out.println(j); System.out.printf("请输入学号 : "); Scanner scanner1 = new Scanner(System.in); String a = scanner1.next(); stu[j].setStunumber(a); System.out.printf("请输入姓名 : "); Scanner scanner2 = new Scanner(System.in); String b = scanner2.next(); stu[j].setName(b); System.out.printf("请输入年龄 : "); Scanner scanner3 = new Scanner(System.in); int c = scanner3.nextInt(); stu[j].setAge(c); System.out.println("请输入性别 : "); System.out.println("1.男生"); System.out.println("2.女生"); Scanner scanner4 = new Scanner(System.in); int d = scanner4.nextInt(); { boolean x; if (d == 2) { x = true; stu[j].setSex(x); } else if (d == 1) { x = false; stu[j].setSex(x); } else { System.out.println("秀吉嘛?"); } } System.out.printf("请输入成绩 : "); Scanner scanner5 = new Scanner(System.in); double e = scanner5.nextDouble(); stu[j].setScore(e); ShowStudent(stu[j]); System.out.println("输入任何数字返回主菜单"); System.out.println("6.退出系统"); System.out.print("请输入:"); int m2; Scanner scanner11 = new Scanner(System.in); m2 = scanner11.nextInt(); if (m2 != 6) { clear(); key = 0; break; } else if (m2 == 6) { key = 6; break; } break; case 3: clear(); int y = 0; int z = 0; int k = 0; String f; System.out.printf("请输入学号:"); Scanner scanner6 = new Scanner(System.in); f = scanner6.next(); for (k = 0; k < 10; k++) { if (deleteStudent(stu[k].getStunumber(), f)) { z = z + 1; } } // System.out.println(z); if (z != 0) { y = 1; } else if (z == 0) { System.out.println("不存在 这个数据。"); } for (k = 0; k < 10; k++) { if (deleteStudent(stu[k].getStunumber(), f)) { stu[k].setName("***"); stu[k].setStunumber("00000000"); stu[k].setScore(0); stu[k].setAge(0); break; } } for (k = 0; k < 10; k++) { if (deleteStudent(stu[k].getStunumber(), f)) { z = z + 1; } } if (y == 1) { System.out.println("删除成功!"); } System.out.println("输入任何数字返回主菜单"); System.out.println("6.退出系统"); System.out.print("请输入:"); int m3; Scanner scanner12 = new Scanner(System.in); m3 = scanner12.nextInt(); if (m3 != 6) { clear(); key = 0; break; } else if (m3 == 6) { key = 6; break; } break; case 4: clear(); break; case 5: clear(); System.out.println("查询方法:"); System.out.println("1.学号 "); System.out.println("2.姓名 "); System.out.println("3.性别 "); System.out.println("4.年龄 "); System.out.printf("请输入:"); int key1; int i; boolean j1 = true; Scanner scanner13 = new Scanner(System.in); key1 = scanner13.nextInt(); switch (key1) { case 1: System.out.printf("请输入学号:"); String stunumber1; Scanner scanner21 = new Scanner(System.in); stunumber1 = scanner21.next(); for (i = 0; i < 10; i++) { if (stu[i].getStunumber().equals(stunumber1)) { ShowStudent(stu[i]); j1 = false; } } if (j1) System.out.println("没有此数据"); // System.out.println(j1); break; case 2: System.out.printf("请输入姓名:"); String name1; Scanner scanner22 = new Scanner(System.in); name1 = scanner22.next(); for (i = 0; i < 10; i++) { if (stu[i].getName().equals(name1)) { ShowStudent(stu[i]); j1 = false; } } if (j1) System.out.println("没有此数据"); break; case 3: int sex1; System.out.println("1.女生 "); System.out.println("2.男生 "); System.out.printf("请输入性别:"); Scanner scanner25 = new Scanner(System.in); sex1 = scanner25.nextInt(); if (sex1 == 1) { for (i = 0; i < 10; i++) { if (stu[i].isSex()) ShowStudent(stu[i]); } } else { for (i = 0; i < 10; i++) { if (stu[i].isSex()) { } else { if (stu[i].getName().equals("***")) { } else { ShowStudent(stu[i]); } } } } break; case 4: System.out.printf("请输入年龄:"); int age1; Scanner scanner24 = new Scanner(System.in); age1 = scanner24.nextInt(); for (i = 0; i < 10; i++) { if (stu[i].getAge() == age1) { ShowStudent(stu[i]); j1 = false; } } if (j1) System.out.println("没有此数据"); break; } System.out.println("输入任何数字返回主菜单"); System.out.println("6.退出系统"); System.out.print("请输入:"); int m5; Scanner scanner15 = new Scanner(System.in); m5 = scanner15.nextInt(); if (m5 != 6) { clear(); key = 0; break; } else if (m5 == 6) { key = 6; break; } break; } } System.out.println(stu[5].getStunumber()); for (int i = 0; i <= 50; i++) { System.out.println(); } } // 第一个方法 public static void ShowStudent(Student stu1) { System.out.printf(" 学号 :" + stu1.getStunumber() + " "); System.out.println(); System.out.printf(" 姓名 :" + stu1.getName() + " "); System.out.println(); System.out.printf(" 年龄 :" + stu1.getAge() + " "); System.out.println(); System.out.printf(" 性别 :" + " "); stu1.getSex(); System.out.printf(" "); System.out.println(); System.out.printf(" 成绩 :" + stu1.getScore() + " "); System.out.println(); System.out.printf("____________________"); System.out.println(); } // 第二个方法 // public static void addStudent ( String stunumber1, String name1, int age1, // boolean sex1, double score1) // { // Scanner scanner1 = new Scanner (System.in ); // age1=scanner1.nextInt(); // } // 第三个方法 public static boolean deleteStudent(String stunumber3, String n) { if (stunumber3.equals(n)) { return true; } else { return false; } } public static void selectStudent() { } // 用来清空控制台的函数 public static void clear() throws AWTException { Robot r = new Robot(); r.mousePress(InputEvent.BUTTON3_MASK); // 按下鼠标右键 r.mouseRelease(InputEvent.BUTTON3_MASK); // 释放鼠标右键 r.keyPress(KeyEvent.VK_CONTROL); // 按下Ctrl键 r.keyPress(KeyEvent.VK_R); // 按下R键 r.keyRelease(KeyEvent.VK_R); // 释放R键 r.keyRelease(KeyEvent.VK_CONTROL); // 释放Ctrl键 r.delay(100); } }
缺少注释这是我的一个缺点,以后会改正的。