輸出的名稱前面總是有一段拼音,這是怎么回事?
輸出的名稱前面總是有一段拼音,這是怎么回事?
下面是代碼內容:
package com.imooc_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> students;
/*
* 在構造器中初始化students屬性
*/
public MapTest() {
this.students = new HashMap<String,Student>();
}
/*
* 測試添加:輸入學生ID,判斷是否被占用
* 若未被占用,則輸入姓名,創建新學生對象,并且
* 添加到students中
*/
public void testPut() {
//創建一個Scanner對象,用來獲取輸入的學生ID和姓名
Scanner console =new Scanner(System.in);
int i=0;
while(i<3) {
System.out.println("請輸入學生ID:");
String ID=console.next();
//判斷ID是否被占用
Student st=students.get(ID);
if(st==null) {
//提示輸入學生姓名
System.out.println("請輸入學生姓名:");
String name=console.next();
//創建新的學生對象
Student newStudent=new Student(ID,name);
//通過調用students的put方法,添加ID-學生映射
students.put(ID, newStudent);
System.out.println("成功添加學生:"+students.get(ID).name);
i++;
}else {
System.out.println("該學生ID已被占用!");
}
}
}
/*
* 測試Map的keySet方法
*/
public void testKeySet() {
//通過keySet方法,返回Map中的所有“鍵”的Set集合
Set<String> keySet=students.keySet();
//取得students的容量
System.out.println("總共有:"+students.size()+"個學生!");
//遍歷keySet,取得Map中的每一個鍵,再調用Map中的get方法取得每一個鍵對應的value值
for(String stuID:keySet) {
Student st=students.get(stuID);
if(st!=null)
System.out.println("學生:"+st.name);
}
}
/*
* 測試刪除Map中的映射
*/
public void testRemove() {
//獲取從鍵盤輸入的待刪除學生ID字符串
Scanner console=new Scanner(System.in);
while(true) {
//提示輸入待刪除的學生的ID
System.out.println("請輸入要刪除的學生的ID");
String ID = console.next();
//判斷該ID是否有對應的學生對象
Student st = students.get(ID);
if(st==null) {
//提示輸入的ID不存在
System.out.println("該ID不存在!");
continue;
}
students.remove(ID);
System.out.println("成功刪除學生:"+st.name);
break;
}
}
/*
* 通過entrySet方法,返回Map中的所有鍵值對
*/
public void testEntrySet() {
//通過entrySet方法,返回Map中的所有鍵值對
Set<Entry<String,Student>> entrySet=students.entrySet();
for(Entry<String,Student> entry:entrySet) {
System.out.println("取得鍵:"+entry.getKey());
System.out.println("對應的值為:"+entry.getValue().name);
}
}
/*
* 利用put方法修改Map中的已有映射
*/
public void testModify() {
//提示輸入要修改的學生ID
System.out.println("請輸入要修改的學生ID:");
Scanner console = new Scanner(System.in);
while(true) {
//取得從鍵盤輸入的學生ID
String stuID = console.next();
//從students中查找該學生ID對應的學生對象
Student student =students.get(stuID);
if(student==null) {
System.out.println("該ID不存在!重新輸入!");
continue;
}
//提示當前對應的學生對象的姓名
System.out.println("當前該學生ID,所對應的學生為:"+student.name);
//提示輸入新的學生姓名,來修改已有的映射
System.out.println("請輸入新的學生姓名");
String name =console.next();
Student newStudent =new Student(stuID,name);
students.put(stuID, newStudent);
System.out.println("修改成功!");
break;
}
}
/*
* 測試Map中,是否包含某個Key值或者某個Value值
*/
public void testContainsKeyOrValue(){
//提示輸入學生id
System.out.println("請輸入要查詢的學生ID:");
Scanner console = new Scanner(System.in);
String id = console.next();
? ?//在Map中,用containsKey()方法,來判斷是否包含某個Key值
System.out.println("您輸入的學生ID為:"+id+",在學生映射表中是否存在:"+
students.containsKey(id));
//用containsValue()方法,來判斷是否包含某個Value值
if(students.containsKey(id))
System.out.println("對應的學生為:"+students.get(id).name);
}
public static void main(String[] args) {
MapTest mt=new MapTest();
mt.testPut();
mt.testKeySet();
// mt.testRemove();
// mt.testEntrySet();
// mt.testModify();
// mt.testEntrySet();
mt.testContainsKeyOrValue();
}
}
2019-09-02
輸入名字的時候鍵入在“請輸入學生name:”這一行就會有問題
2019-07-25
別的不知道? ?反正你至少少了CONTINUE;?
2019-07-22
我用老師的代碼也會這樣 不是代碼的問題 直接用復制課程名稱粘貼反而就可以了。。
2019-03-06
我也想問!?。?/p>