0
我在這裏有一個工資計劃,我希望能夠將對象添加到Employee類。目前我定義了與Employee類相關的對象emp。如果我想在我的腳本中輸入STOP命令之前能夠動態創建對象,該怎麼辦?另外,一旦完成,我怎麼能打印所有與Employee類相關的對象。提前致謝。創建可變數量的對象並顯示java中的所有對象
import java.util.Scanner;
class PayrollProgram
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
List<Employee> employees = new ArrayList<>();
while (!emp.name.equals("STOP"))
{
Employee emp = new Employee();
System.out.print("Employee's Name: ");
emp.name = scan.next();
if(emp.name.equals("STOP"))
{
System.out.printf("The Application is STOPPING......");
break;
}
System.out.print("Enter hourly wage: $ ");
emp.wage = scan.nextDouble();
while (emp.wage < 0)
{
System.out.printf("Please Enter a Positive Number! \n");
System.out.print("Enter hourly wage: $ ");
emp.wage = scan.nextDouble();
}
System.out.print("Hours Worked in Week: ");
emp.hours = scan.nextDouble();
while (emp.hours < 0)
{
System.out.printf("Please Enter a Positive Number! \n");
System.out.print("Hours Worked in Week: ");
emp.hours = scan.nextDouble();
}
employees.add(emp);
emp.printEmployee();
}
for(Employee emp : employees)
{
System.outprintln(emp.name);
}
}
}
我在運行時收到「無法找到符號」錯誤。我必須申報清單嗎? – AwayFromMyDesk
用代碼編輯你的文章,我不能沒有看到它。 –
已編輯。謝謝。 – AwayFromMyDesk