關(guān)于已知學(xué)生名字,修改其ID。希望大神可以指點我一下。。。
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("請輸入要添加學(xué)生的學(xué)生的ID:"); str1?=?input.next(); if?(student.get(str1)?==?null)?{ System.out.println("請輸入學(xué)生姓名:"); str2?=?input.next(); Student?stu?=?new?Student(str1,?str2); student.put(str1,?stu); System.out.println("添加成功!\n還要繼續(xù)添加嗎?(t/f):"); choice?=?input.next(); }?else?{ System.out.println("您輸入的ID已占用,請重新輸入!"); continue; } } System.out.println("添加了一下學(xué)生:"); printStu(); } public?void?modifyStu(){ String?str1,str2,choice="t"; // while(choice.equalsIgnoreCase("t")){ // System.out.println("請輸入要修改的學(xué)生ID:"); // ??str1=input.next(); // if(student.containsKey(str1)){ // System.out.println("請輸入學(xué)生姓名:"); // ??str2=input.next(); // Student?stu1=new?Student(str1,str2); // student.put(str1,?stu1); // System.out.println("還要繼續(xù)修改嗎?"); // choice=input.next(); // }else?{ // System.out.println("您輸入的ID不存在!"); // continue; // } // ?? // } choice="t"; ????int?i=0; while(choice.equalsIgnoreCase("t")){ System.out.println("請輸入要修改的學(xué)生名字:"); 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("還要繼續(xù)修改嗎?"); choice=input.next(); break; } } if(i==0){ System.out.println("您輸入的學(xué)生名字不存在!"); continue; } } printStu(); } public?void?printStu(){ Set<Entry<String,?Student>>?stu=student.entrySet(); for(Entry<String,?Student>?s:stu){ System.out.println("學(xué)生ID:"+s.getKey()+"?學(xué)生名字:"+(s.getValue()).getName()); } } }
問號那里改填什么?
2016-05-10
? ? ? ? while(choice.equalsIgnoreCase("t")){
? ? ? ? ? ? System.out.println("請輸入要修改的學(xué)生名字:");
? ? ? ? ? ? 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("還要繼續(xù)修改嗎?");
? ? ? ? ? ? ? ? ? ? choice=input.next();
? ? ? ? ? ? ? ? ? ? i = 1;
? ? ? ? ? ? ? ? ? ? if (choice.equals("f")) {
? ? ? ? ? ? ? ? ? ? student.put("1", new Student(str1, str2));
? ? ? ? ? ? ? ? ? ? break;
}
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if(i==0){
? ? ? ? ? ? ? ? System.out.println("您輸入的學(xué)生名字不存在!");
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? }
2016-05-10
這是全部的代碼,你那個printStu中的s.getKey()是得到的類似于數(shù)組下標(biāo)的東西,只有得到下標(biāo)對應(yīng)元素的ID才是學(xué)生的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("請輸入要添加學(xué)生的學(xué)生的ID:");
? ? ? ? ? ? str1 = input.next();
? ? ? ? ? ? if (student.get(str1) == null) {
? ? ? ? ? ? ? ? System.out.println("請輸入學(xué)生姓名:");
? ? ? ? ? ? ? ? str2 = input.next();
? ? ? ? ? ? ? ? Student stu = new Student(str1, str2);
? ? ? ? ? ? ? ? student.put(str1, stu);
? ? ? ? ? ? ? ? System.out.println("添加成功!\n還要繼續(xù)添加嗎?(t/f):");
? ? ? ? ? ? ? ? choice = input.next();
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? System.out.println("您輸入的ID已占用,請重新輸入!");
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? ?
? ? ? ? }
? ? ? ? System.out.println("添加了一下學(xué)生:");
? ? ? ? printStu();
? ? }
? ? public void modifyStu(){
? ? ? ? String str1,str2,choice="t";
// ? ? ?while(choice.equalsIgnoreCase("t")){
// ? ? ?System.out.println("請輸入要修改的學(xué)生ID:");
// ? ? ? ?str1=input.next();
// ? ? ?if(student.containsKey(str1)){
// ? ? ? ? ?System.out.println("請輸入學(xué)生姓名:");
// ? ? ? ? ? ?str2=input.next();
// ? ? ? ? ?Student stu1=new Student(str1,str2);
// ? ? ? ? ?student.put(str1, stu1);
// ? ? ? ? ?System.out.println("還要繼續(xù)修改嗎?");
// ? ? ? ? ?choice=input.next();
// ? ? ? ? ?}else {
// ? ? ? ? ? ? ?System.out.println("您輸入的ID不存在!");
// ? ? ? ? ? ? ?continue;
// ? ? ? ? ?}
// ? ? ? ?
// ? ? ?} ?
? ? ? ? choice="t";
? ? ? ? int i=0;
? ? ? ? while(choice.equalsIgnoreCase("t")){
? ? ? ? ? ? System.out.println("請輸入要修改的學(xué)生名字:");
? ? ? ? ? ? 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("還要繼續(xù)修改嗎?");
? ? ? ? ? ? ? ? ? ? choice=input.next();
? ? ? ? ? ? ? ? ? ? i = 1;
? ? ? ? ? ? ? ? ? ? if (choice.equals("f")) {
? ? ? ? ? ? ? ? ? ? student.put(s.getId(), new Student(str1, str2));
? ? ? ? ? ? ? ? ? ? break;
}
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if(i==0){
? ? ? ? ? ? ? ? System.out.println("您輸入的學(xué)生名字不存在!");
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? printStu();
? ? }
? ? public void printStu(){
? ? ? ? Set<Entry<String, Student>> stu=student.entrySet();
? ? ? ? for(Entry<String, Student> s:stu){
? ? ? ? ? ? System.out.println("學(xué)生ID:"+s.getValue().getId()+" 學(xué)生名字:"+(s.getValue()).getName());
? ? ? ? }
? ? }
? ? ?
}
2016-05-10
這是iterator,和for語句的輸出結(jié)果。
2016-05-10
原來是printStu出了問題。。。。但是為什么注釋掉的for語句只能打印輸出原來沒修改的呢?
2016-05-10
奇怪的是,我的getid()方法在這個循環(huán)就不能用了,但是
就可以得到id,是什么原因呢?
2016-05-10
因為我的學(xué)生類,ID設(shè)為private的,所以用的get方法。。
2016-05-10
? ? ? ? while(choice.equalsIgnoreCase("t")){
? ? ? ? ? ? System.out.println("請輸入要修改的學(xué)生名字:");
? ? ? ? ? ? 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("還要繼續(xù)修改嗎?");
? ? ? ? ? ? ? ? ? ? choice=input.next();
? ? ? ? ? ? ? ? ? ? i = 1;
? ? ? ? ? ? ? ? ? ? if (choice.equals("f")) {
? ? ? ? ? ? ? ? ? ? student.put(s.Id, new Student(str1, str2));
? ? ? ? ? ? ? ? ? ? break;
}
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if(i==0){
? ? ? ? ? ? ? ? System.out.println("您輸入的學(xué)生名字不存在!");
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? }
剛剛那個錯了,那個成了只修改映射中ID為1的學(xué)生ID了,這個就沒問題了
2016-05-10
運行結(jié)果