这是我的代码:
import java.util.Scanner;
public class empMod
{
public static void main(String[] args)
{
int choice;
Scanner input = new Scanner(System.in);
do
{
choice = -1;
System.out.println("Employee Data:");
System.out.println("1. - Employee Name:");
System.out.println("2. - Employee Hire Date:");
System.out.println("3. - Employee Address:");
System.out.println("4. - Employee Number:");
System.out.println("5. - Exit");
choice = input.nextInt();
input.nextLine();
switch (choice)
{
case 1:
String empName = new String ();
System.out.println("Enter the name of the employee:");
String name = input.nextLine();
break;
case 2:
String empDate = new String ();
System.out.println("Enter the hire date of the employee:");
String date = input.nextLine();
break;
case 3:
String empAddress = new String ();
System.out.println("Enter the address of the employee:");
String address = input.nextLine();
break;
case 4:
String empNumb = new String ();
System.out.println("Enter the Employee number:");
int number = input.nextInt();
break;
case 5:
System.out.print("\n");
System.out.println("The name of the employee is: " + empName); // <-- This is the line where the error occurs.
break;
default:
continue;
}
}
while (choice != 6);
}
}
该程序的目的是让用户输入有关员工的信息,然后根据要求显示信息。当我去编译程序时,我收到以下错误:
empMod.java:57: error: variable empName might not have been initialized
System.out.println("The name of the employee is:
" + empName);
^
字符串变量在另一种情况下被初始化,所以我不确定这个问题。