我正在写一个小电话簿,只是为了练习我对 java 的了解。问题出在下面的代码中,我希望代码在调用 getname 方法时显示名称和号码。我是一个初学者,我真的很想让这个项目成功。
public class Phone {
private HashMap<String, Integer> name;
HashMap<String, Integer> namelist = new HashMap<String, Integer>();
Integer number;
String names;
/**
* @return the name
*/
Phone() {
// get user to enter name and number
Scanner scan = new Scanner(System.in);
System.out.println("enter name ");
names = scan.nextLine();
System.out.println("enter number ");
String nums = scan.nextLine();
number = Integer.parseInt(nums);
namelist.put(names, number); // set key and value to namelist object;
setName(namelist); //
}
void printValue() {
System.out.print(namelist.get(names));
}
public HashMap<String, Integer> getName() {
return name;
}
/**
* @param names
* the name to set
*/
public void setName(HashMap<String, Integer> names) {
this.name = names;
}
public static void main(String[] args) {
Phone ph = new Phone();
Gui window = new Gui();
System.out.println(ph.getName());
ph.printValue();
}
}