提问人:MiakaSHW 提问时间:9/10/2022 最后编辑:MiakaSHW 更新时间:9/24/2022 访问量:29
我需要将我的 HashMap 移动到哪里,以便两个单独的 Switch 语句可以看到它?[复制]
Where do I need to move my HashMap so that two separate Switch Statements can see it? [duplicate]
问:
这个问题在这里已经有答案了:
“找不到符号”或“无法解析符号”错误是什么意思? (18 个回答)
Java 中的“范围”是什么? (2 个答案)
去年关闭。
这篇文章是去年编辑并提交审查的,但未能重新打开该帖子:
原始关闭原因未解决
我正在完成我正在学习的 Java 课程的期末项目。该项目的目标是创建一个资产管理工具,用户可以使用它来输入员工及其员工 ID、分配给他们的电子设备和序列号,然后通过搜索他们的姓名或员工 ID 来查找员工拥有的设备。
我有一个 Switch 语句,用于检查用户是否键入了“添加新员工记录”、“搜索员工记录”、“结束程序”或其他任何内容(默认)。我需要创建第二个开关,当用户键入“搜索员工记录”时,该开关将运行。我正在对此进行研究,发现将 Switch 语句嵌套在其他 Switch 语句中不被认为是最佳实践,最好编写一个可以在特定 Switch 案例中调用的方法,该方法将运行另一个 Switch 语句。我已经尝试过,但是我用此方法编写的代码无法看到在主方法开始时创建的 HashMap。我收到的所有错误都与此有关。
问:我需要将我的 HashMap 移动到哪里,以便第一个 Switch(位于 main 类,main 方法)和第二个 Switch(位于 Employee 类,searchForEmployee 方法中)都可以查看并从中收集键/值?
这是我在主类中的代码:
import java.util.*;
class Main {
public static void main(String[] args) {
//Creating a Scanner to take input from user
Scanner input = new Scanner(System.in);
//Creating a HashMap to take in employee names and ID numbers
Map<String, Integer> employeeIndex = new HashMap<>();
//Creating an ArrayList to keep track of employees and allow for easy user searching
ArrayList<Employee> employees = new ArrayList<Employee>();
System.out.println("This is an Asset Management Tracking program. This database allows you to add and manage employee-assigned equipment. \n");
boolean loop = true;
while (loop) {
System.out.println("\nWould you like to add an employee to the system, or search for an employee record? Please enter your selection below: \n" + "\nAdd New Employee Record \n" + "\nSearch Employee Records \n" + "\nEnd Program \n");
String answer = input.nextLine().toLowerCase();
switch (answer) {
case "add new employee record": System.out.println("\nPlease enter the employee's name (Last name, First name): \n");
String employeeName = input.nextLine();
System.out.println("\nPlease enter their 6-digit employee ID: \n");
int employeeID = input.nextInt();
input.nextLine();
Employee employee1 = new Employee(employeeName, employeeID);
employeeIndex.put(employeeName, employeeID);
System.out.println("\nOkay. You have entered:\n\n" + "Name: " + employeeName + "\nID: " + employeeID);
employees.add(employee1);
System.out.println(employeeIndex.entrySet());
break;
case "search employee records": System.out.println("\nHow would you like to search? \n" + "\nEmployee Name\n" + "\nEmployee ID\n");
Employee.searchForEmployee();
break;
case "end program":
System.out.println("\nThank you for using this Asset Management Program!");
input.close();
loop = false;
break;
default:
System.out.println("\nSorry, that is an incorrect response (perhaps you typed it wrong?). Please choose one of the available options. \n");
break;
}
}
}
}
以及 Employee 类中的 searchForEmployee 方法:
static void searchForEmployee() {
Scanner input2 = new Scanner(System.in);
String answer2 = input2.next().toLowerCase();
switch (answer2) {
case "employee name":
System.out.println("\nPlease enter the employee's name (Last name, First name): \n");
String searchEmployeeName = input2.nextLine();
if (employeeIndex.containsKey(searchEmployeeName) = true) {
System.out.println(employeeIndex.get(searchEmployeeName));
} else {
System.out.println("\nI'm sorry, that employee does not appear to be in this database yet. Here are the employee records contained so far: \n");
System.out.println(employeeIndex.keySet());
}
break;
case "employee id":
System.out.println("\nPlease enter the employee's ID#: \n");
int searchEmployeeID = input2.nextInt();
if (employeeIndex.containsValue(searchEmployeeID) = true) {
System.out.println(employeeIndex.get());
} else {
System.out.println("\nI'm sorry, that employee ID does not appear to be in this database yet. Here are the employee ID numbers contained so far: \n");
System.out.println(employeeIndex.values());
}
break;
default:
System.out.println("\nSorry, that is an incorrect response (perhaps you typed it wrong?). Please choose one of the available options. \n");
break;
}
}
这些是我在运行程序时收到的错误:
Employee.java:26: error: cannot find symbol
if (employeeIndex.containsKey(searchEmployeeName) = true) {
^
symbol: variable employeeIndex
location: class Employee
Employee.java:27: error: cannot find symbol
System.out.println(employeeIndex.get(searchEmployeeName));
^
symbol: variable employeeIndex
location: class Employee
Employee.java:30: error: cannot find symbol
System.out.println(employeeIndex.keySet());
^
symbol: variable employeeIndex
location: class Employee
Employee.java:37: error: cannot find symbol
if (employeeIndex.containsValue(searchEmployeeID) = true) {
^
symbol: variable employeeIndex
location: class Employee
Employee.java:38: error: cannot find symbol
System.out.println(employeeIndex.get());
^
symbol: variable employeeIndex
location: class Employee
Employee.java:41: error: cannot find symbol
System.out.println(employeeIndex.values());
^
symbol: variable employeeIndex
location: class Employee
6 errors
exit status 1
再次感谢您的帮助。我仍然处于初学者阶段,这个社区在学习过程中对我非常有帮助。
答: 暂无答案
下一个:R 更改嵌套列表的类型
评论