如果繁星能来到我身边
2016-05-10 17:35
import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; public class MapTest { public Map<String, Student> student; Scanner input = new Scanner(System.in); public MapTest() { student = new HashMap<String, Student>(); } public static void main(String[] args) { MapTest mapTest=new MapTest(); mapTest.mapAddStu(); mapTest.modifyStu(); } public void mapAddStu() { String choice = "t"; String str1,str2; while (choice.equalsIgnoreCase("t")) { System.out.println("請輸入要添加學生的學生的ID:"); str1 = input.next(); if (student.get(str1) == null) { System.out.println("請輸入學生姓名:"); str2 = input.next(); Student stu = new Student(str1, str2); student.put(str1, stu); System.out.println("添加成功!\n還要繼續添加嗎?(t/f):"); choice = input.next(); } else { System.out.println("您輸入的ID已占用,請重新輸入!"); continue; } } System.out.println("添加了一下学生:"); printStu(); } public void modifyStu(){ String str1,str2,choice="t"; // while(choice.equalsIgnoreCase("t")){ // System.out.println("请输入要修改的学生ID:"); // str1=input.next(); // if(student.containsKey(str1)){ // System.out.println("请输入学生姓名:"); // str2=input.next(); // Student stu1=new Student(str1,str2); // student.put(str1, stu1); // System.out.println("还要继续修改吗?"); // choice=input.next(); // }else { // System.out.println("您输入的ID不存在!"); // continue; // } // // } choice="t"; int i=0; while(choice.equalsIgnoreCase("t")){ System.out.println("请输入要修改的学生名字:"); str2=input.next(); Collection<Student> stu=student.values(); for(Student s:stu){ if(str2.equals(s.getName())){ System.out.println("请输入要修改的ID:"); str1=input.next(); //???????????????? i=1; System.out.println("还要继续修改吗?"); choice=input.next(); break; } } if(i==0){ System.out.println("您输入的学生名字不存在!"); continue; } } printStu(); } public void printStu(){ Set<Entry<String, Student>> stu=student.entrySet(); for(Entry<String, Student> s:stu){ System.out.println("学生ID:"+s.getKey()+" 学生名字:"+(s.getValue()).getName()); } } }
问号那里改填什么?
while(choice.equalsIgnoreCase("t")){
System.out.println("请输入要修改的学生名字:");
str2=input.next();
Collection<Student> stu=student.values();
for(Student s:stu){
if(str2.equals(s.Name)){
System.out.println("请输入要修改的ID:");
str1=input.next();
System.out.println("还要继续修改吗?");
choice=input.next();
i = 1;
if (choice.equals("f")) {
student.put("1", new Student(str1, str2));
break;
}
}
}
if(i==0){
System.out.println("您输入的学生名字不存在!");
continue;
}
}
这是全部的代码,你那个printStu中的s.getKey()是得到的类似于数组下标的东西,只有得到下标对应元素的ID才是学生的ID,那会改了忘了贴出来了
图片是更改后的测试
---------我也改了为私有类型的------------
private String Id;
private String Name;
public Student(String Id, String Name){
this.Id = Id;
this.Name = Name;
this.courses = new HashSet();
}
public String getId(){
return this.Id;
}
public void setId(String ID){
this.Id = ID;
}
public void setName(String name){
this.Name = name;
}
public String getName(){
return this.Name;
}
---------你再对照看看下面的--------
package collection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class test {
public Map<String, Student> student;
Scanner input = new Scanner(System.in);
public test() {
student = new HashMap<String, Student>();
}
public static void main(String[] args) {
test mapTest=new test();
mapTest.mapAddStu();
mapTest.modifyStu();
}
public void mapAddStu() {
String choice = "t";
String str1,str2;
while (choice.equalsIgnoreCase("t")) {
System.out.println("請輸入要添加學生的學生的ID:");
str1 = input.next();
if (student.get(str1) == null) {
System.out.println("請輸入學生姓名:");
str2 = input.next();
Student stu = new Student(str1, str2);
student.put(str1, stu);
System.out.println("添加成功!\n還要繼續添加嗎?(t/f):");
choice = input.next();
} else {
System.out.println("您輸入的ID已占用,請重新輸入!");
continue;
}
}
System.out.println("添加了一下学生:");
printStu();
}
public void modifyStu(){
String str1,str2,choice="t";
// while(choice.equalsIgnoreCase("t")){
// System.out.println("请输入要修改的学生ID:");
// str1=input.next();
// if(student.containsKey(str1)){
// System.out.println("请输入学生姓名:");
// str2=input.next();
// Student stu1=new Student(str1,str2);
// student.put(str1, stu1);
// System.out.println("还要继续修改吗?");
// choice=input.next();
// }else {
// System.out.println("您输入的ID不存在!");
// continue;
// }
//
// }
choice="t";
int i=0;
while(choice.equalsIgnoreCase("t")){
System.out.println("请输入要修改的学生名字:");
str2=input.next();
Collection<Student> stu=student.values();
for(Student s:stu){
if(str2.equals(s.getName())){
System.out.println("请输入要修改的ID:");
str1=input.next();
System.out.println("还要继续修改吗?");
choice=input.next();
i = 1;
if (choice.equals("f")) {
student.put(s.getId(), new Student(str1, str2));
break;
}
}
}
if(i==0){
System.out.println("您输入的学生名字不存在!");
continue;
}
}
printStu();
}
public void printStu(){
Set<Entry<String, Student>> stu=student.entrySet();
for(Entry<String, Student> s:stu){
System.out.println("学生ID:"+s.getValue().getId()+" 学生名字:"+(s.getValue()).getName());
}
}
}
这是iterator,和for语句的输出结果。
while(choice.equalsIgnoreCase("t")){ System.out.println("请输入要修改的学生名字:"); str2=input.next(); Collection<Student> stu=student.values(); for(Student s:stu){ if(str2.equals(s.getName())){ System.out.println("请输入要修改的ID:"); str1=input.next(); System.out.println("还要继续修改吗?"); choice=input.next(); i = 1; if (choice.equals("f")) { student.put(s.getId(), new Student(str1, str2)); System.out.println("修改成功"); break; } } } if(i==0){ System.out.println("您输入的学生名字不存在!"); continue; } printStu(); } public void printStu(){ Set<Entry<String, Student>> stu=student.entrySet(); Iterator<Entry<String, Student>> it=stu.iterator(); while(it.hasNext()){ Entry c=(Entry) it.next(); System.out.println(((Student)c.getValue()).getId() +" "+((Student)c.getValue()).getName()); } // for(Entry<String, Student> s:stu){ // System.out.println("学生ID:"+s.getKey()+" 学生名字:"+(s.getValue()).getName()); // } }
原来是printStu出了问题。。。。但是为什么注释掉的for语句只能打印输出原来没修改的呢?
奇怪的是,我的getid()方法在这个循环就不能用了,但是
public static void main(String[] args) { MapTest mapTest=new MapTest(); //mapTest.mapAddStu(); //mapTest.modifyStu(); Student stu1=new Student("a","s"); System.out.println(stu1.getId()); }
就可以得到id,是什么原因呢?
因为我的学生类,ID设为private的,所以用的get方法。。
while(choice.equalsIgnoreCase("t")){
System.out.println("请输入要修改的学生名字:");
str2=input.next();
Collection<Student> stu=student.values();
for(Student s:stu){
if(str2.equals(s.Name)){
System.out.println("请输入要修改的ID:");
str1=input.next();
System.out.println("还要继续修改吗?");
choice=input.next();
i = 1;
if (choice.equals("f")) {
student.put(s.Id, new Student(str1, str2));
break;
}
}
}
if(i==0){
System.out.println("您输入的学生名字不存在!");
continue;
}
}
刚刚那个错了,那个成了只修改映射中ID为1的学生ID了,这个就没问题了
运行结果
Java入门第三季
409764 学习 · 4543 问题
相似问题