用java写一个小程序【用一个学生类:两个属性(学号和姓名)和集合的类来实现面的效果
1个回答
2014-01-22
展开全部
嗯,给你写了一个,拿去吧:import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;public class test {
static BufferedReader bufReader = new BufferedReader(new InputStreamReader(
System.in));
static Map<String, student> students = new HashMap<String, student>(); public static void main(String argv[]) throws IOException {
System.out.println("*********学员管理系统**********\n" + "1.添加学员信息\n"
+ "2.查询学员信息\n" + "3.删除学员信息\n" + "4.显示所有学员信息\n" + "0.退出系统");
while (true) {
System.out.println("请输入系统选项(0-4):");
String cmd = bufReader.readLine();
if ("1".equals(cmd)) {
System.out.println("请输入学号:");
String id = bufReader.readLine();
System.out.println("请输入姓名:");
String name = bufReader.readLine();
students.put(id, new student(id, name));
} else if ("2".equals(cmd)) {
System.out.println("请输入要查询的学号:");
String id = bufReader.readLine();
if (students.containsKey(id)) {
System.out.println("学号:" + students.get(id).getStuid()
+ "\t姓名:" + students.get(id).getName());
} else {
System.out.println("没找到");
}
} else if ("3".equals(cmd)) {
System.out.println("请输入要删除的学号:");
String id = bufReader.readLine();
if (students.containsKey(id)) {
students.remove(id);
System.out.println("删除成功");
} else {
System.out.println("没找到,删除失败");
}
} else if ("4".equals(cmd)) {
System.out.print("[");
for (String id : students.keySet()) { System.out.print(" 学号:" + students.get(id).getStuid()
+ " , 姓名:" + students.get(id).getName()+" ");
}
System.out.println("]");
} else {
return;
}
} } public static class student {
private String stuid;
private String name; public String getStuid() {
return stuid;
} public String getName() {
return name;
} public student(String id, String name) {
this.stuid = id;
this.name = name;
}
}}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;public class test {
static BufferedReader bufReader = new BufferedReader(new InputStreamReader(
System.in));
static Map<String, student> students = new HashMap<String, student>(); public static void main(String argv[]) throws IOException {
System.out.println("*********学员管理系统**********\n" + "1.添加学员信息\n"
+ "2.查询学员信息\n" + "3.删除学员信息\n" + "4.显示所有学员信息\n" + "0.退出系统");
while (true) {
System.out.println("请输入系统选项(0-4):");
String cmd = bufReader.readLine();
if ("1".equals(cmd)) {
System.out.println("请输入学号:");
String id = bufReader.readLine();
System.out.println("请输入姓名:");
String name = bufReader.readLine();
students.put(id, new student(id, name));
} else if ("2".equals(cmd)) {
System.out.println("请输入要查询的学号:");
String id = bufReader.readLine();
if (students.containsKey(id)) {
System.out.println("学号:" + students.get(id).getStuid()
+ "\t姓名:" + students.get(id).getName());
} else {
System.out.println("没找到");
}
} else if ("3".equals(cmd)) {
System.out.println("请输入要删除的学号:");
String id = bufReader.readLine();
if (students.containsKey(id)) {
students.remove(id);
System.out.println("删除成功");
} else {
System.out.println("没找到,删除失败");
}
} else if ("4".equals(cmd)) {
System.out.print("[");
for (String id : students.keySet()) { System.out.print(" 学号:" + students.get(id).getStuid()
+ " , 姓名:" + students.get(id).getName()+" ");
}
System.out.println("]");
} else {
return;
}
} } public static class student {
private String stuid;
private String name; public String getStuid() {
return stuid;
} public String getName() {
return name;
} public student(String id, String name) {
this.stuid = id;
this.name = name;
}
}}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询