我檢查了代碼并在鍵入 ADD 時將資料保存到 HashMap 是正確的。然后在選擇選項 FIND 后,我可以進入專用功能,但該方法無法向我顯示找到的物件,即使它 100% 正確。請檢查此代碼并告訴我為什么它沒有在“public void showInfo(String name, String secondName)”中找到由類 CompanyApp 中的 TYPING“FIND”引發的類 Company 的正確物件
import java.util.InputMismatchException;
import java.util.Objects;
import java.util.Scanner;
public class CompanyApp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
options[] values = options.values();
int choose;
int EXIT_NR = 2;
Company ref = new Company();
do {
System.out.println("Available options: ");
for (options one : values) {
System.out.println(one.getDescription() " - " one.name());
}
System.out.println("Choose one: ");
try {
choose = options.valueOf(in.nextLine()).ordinal();
if (Objects.equals(EXIT_NR, choose)) break;
if (choose < 0 || choose >= options.values().length) {
throw new IllegalArgumentException("Choose 0, 1 or 2!");
}
options(choose);
} catch (InputMismatchException e) {
System.out.println("Choose a number ");
}
} while (1 == 1);
}
static void options(int choose){
Company ref = new Company();
Scanner info = new Scanner(System.in);
switch (choose){
case 0:
System.out.println("Type in the name of the worker: ");
String name = info.nextLine();
System.out.println("Type in the second name of the worker: ");
String secondName = info.nextLine();
System.out.println("Type in the salary: ");
double salary = info.nextDouble();
info.nextLine();
ref.add(new Employee(name, secondName, salary));
break;
case 1:
System.out.println("Type in the name of the worker you want to find: ");
String name2 = info.nextLine();
System.out.println("Type in the second name of the worker you want to
find: ");
String secondName2 = info.nextLine();
ref.showInfo(name2, secondName2);
break;
}
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Company {
private Map<String, Employee> map = new HashMap<>();
public void add(Employee employee){
String key = employee.getName() " " employee.getSecondName();
if(!map.containsKey(key)){
map.put(key, employee);
System.out.println("Added object to map");}
}
public void showInfo(String name, String secondName){
String key = name " " secondName;
System.out.println("in showinfo method");
if(map.containsKey(key)){
System.out.println("found an object");
Employee employee = map.get(key);
System.out.println(employee.getName());
}}}
enum options {
ADD("Add employee "), FIND("Find employee"), EXIT("Exit program");
private String description;
options(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "options{"
"description='" description '\''
'}';
}
}
String name;
String secondName;
double salary;
public Employee(String name, String secondName, double salary) {
this.name = name;
this.secondName = secondName;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{"
"name='" name '\''
", secondName='" secondName '\''
", salary=" salary
'}';
}
}
uj5u.com熱心網友回復:
問題出在方法上static void options(int choose)。您需要傳遞Company-Object 并在那里使用它,如下所示:
從 main 方法呼叫(ref是Company您在 main 方法中創建的-Object)
options(choose, ref);
options帶有Company第二個引數的-method :
static void options(int choose, Company ref){
Scanner info = new Scanner(System.in);
switch (choose){
case 0:
System.out.println("Type in the name of the worker: ");
String name = info.nextLine();
System.out.println("Type in the second name of the worker: ");
String secondName = info.nextLine();
System.out.println("Type in the salary: ");
double salary = info.nextDouble();
info.nextLine();
//use the passed Company here
ref.add(new Employee(name, secondName, salary));
break;
case 1:
System.out.println("Type in the name of the worker you want to find: ");
String name2 = info.nextLine();
System.out.println("Type in the second name of the worker you want to find: ");
String secondName2 = info.nextLine();
//and here
ref.showInfo(name2, secondName2);
break;
}
}
解釋您的代碼中發生了什么
如前所述,問題出在方法上static void options(int choose)。
在這里,您創建了一個新的Company-Object,它不會以任何方式傳遞給 main 方法。
當您使用 ADD 和 FIND 之后會發生這種情況:
options從主方法呼叫ADD- 新的
Company物件是在options - new
Employee-ObjectCompany從上一點添加到 - 方法結束 -> 創建的
Company-Object 被“扔掉”(符合垃圾收集條件) options從主方法呼叫FIND- new
Company-Object 是在其中創建的options(因此其中沒有員工) - 沒有
Employee可以找到,因為新創建的地圖中沒有條目Company
uj5u.com熱心網友回復:
當您嘗試使用 FIND 選項從中獲取資料時,該地圖是空的。原因是您在 options 方法中重新創建了 Company 物件:
Company ref = new Company();
與此同時,地圖也被重新創建,所以里面沒有記錄。
此外,未使用 main 方法中的 Company 物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/358210.html
上一篇:用段落中的常用詞制作詞典
