为了账号安全,请及时绑定邮箱和手机立即绑定

关于已知学生名字,修改其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("請輸入要添加學生的學生的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());
		}
	}
	
}

问号那里改填什么?

正在回答

8 回答

        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;

            }

        }


0 回复 有任何疑惑可以回复我~
#1

如果繁星能来到我身边 提问者

...你并没有写出来
2016-05-10 回复 有任何疑惑可以回复我~
#2

如果繁星能来到我身边 提问者

看错了不好意思。谢谢你!!~~
2016-05-10 回复 有任何疑惑可以回复我~
#3

如果繁星能来到我身边 提问者

非常感谢!
2016-05-10 回复 有任何疑惑可以回复我~


这是全部的代码,你那个printStu中的s.getKey()是得到的类似于数组下标的东西,只有得到下标对应元素的ID才是学生的ID,那会改了忘了贴出来了

图片是更改后的http://img1.sycdn.imooc.com//5731d7490001091119201080.jpg测试

---------我也改了为私有类型的------------

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());

        }

    }

     

}


0 回复 有任何疑惑可以回复我~

http://img1.sycdn.imooc.com//5731d43e0001835e03150518.jpg

这是iterator,和for语句的输出结果。

0 回复 有任何疑惑可以回复我~
#1

拥抱每天最早的阳光

你获取的都是Map映射中的下标,不是那个位置的学生的ID
2016-05-10 回复 有任何疑惑可以回复我~
#2

如果繁星能来到我身边 提问者 回复 拥抱每天最早的阳光

总算明白了,谢谢大神解惑!
2016-05-11 回复 有任何疑惑可以回复我~
  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语句只能打印输出原来没修改的呢?

0 回复 有任何疑惑可以回复我~

http://img1.sycdn.imooc.com//5731c02a0001d55602850364.jpg

奇怪的是,我的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,是什么原因呢?

0 回复 有任何疑惑可以回复我~

http://img1.sycdn.imooc.com//5731bbfd0001ff3c07190387.jpg

因为我的学生类,ID设为private的,所以用的get方法。。

http://img1.sycdn.imooc.com//5731bc470001147503090389.jpg

0 回复 有任何疑惑可以回复我~

        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了,这个就没问题了

http://img1.sycdn.imooc.com//5731b7130001b0db19201080.jpg

0 回复 有任何疑惑可以回复我~
#1

如果繁星能来到我身边 提问者

为什么我就不可以呢。。。
2016-05-10 回复 有任何疑惑可以回复我~
#2

如果繁星能来到我身边 提问者

麻烦高手再帮我看看,我的疑问在下面的回复里面。。。
2016-05-10 回复 有任何疑惑可以回复我~

运行结果

http://img1.sycdn.imooc.com//5731b5fc0001630710000562.jpg


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

关于已知学生名字,修改其ID。希望大神可以指点我一下。。。

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号