创建数量可变的对象,并在java中显示所有对象
我这里有一个工资单程序,我希望能够向Employee类添加对象。目前,我定义了与Employee类相关的对象emp。如果我希望在脚本中输入STOP命令之前能够动态创建对象,该怎么办。此外,完成后,如何打印与Employee类相关的所有对象。提前谢谢创建数量可变的对象,并在java中显示所有对象,java,Java,我这里有一个工资单程序,我希望能够向Employee类添加对象。目前,我定义了与Employee类相关的对象emp。如果我希望在脚本中输入STOP命令之前能够动态创建对象,该怎么办。此外,完成后,如何打印与Employee类相关的所有对象。提前谢谢 import java.util.Scanner; class PayrollProgram { public static void main(String[] args) { Scanner scan = new Scanner(S
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);
}
}
}
import java.util.Scanner;
班级工资计划
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
List employees=new ArrayList();
而(!emp.name.equals(“STOP”))
{
员工emp=新员工();
系统输出打印(“员工姓名:”);
emp.name=scan.next();
如果(emp.name.equals(“STOP”))
{
System.out.printf(“应用程序正在停止……”);
打破
}
系统输出打印(“输入小时工资:$”;
emp.wage=scan.nextDouble();
而(emp.工资<0)
{
System.out.printf(“请输入正数!\n”);
系统输出打印(“输入小时工资:$”;
emp.wage=scan.nextDouble();
}
System.out.print(“每周工作时数:”);
emp.hours=scan.nextDouble();
而(环境时数<0)
{
System.out.printf(“请输入正数!\n”);
System.out.print(“每周工作时数:”);
emp.hours=scan.nextDouble();
}
添加(emp);
emp.printEmployee();
}
对于(员工emp:员工)
{
系统输出ln(emp.name);
}
}
}
保留一个包含每个员工的数据结构(在本例中通常是一个列表)。每个循环只需创建一个新员工并将其添加到列表中。之后,你可以打印所有内容
List<Employee> employees = new ArrayList<>();
while (!emp.name.equals("STOP")) {
Employee emp = new Employee();
// read all the data from input
employees.add(emp);
}
for(Employee emp : employees) {
System.out.println(emp.name);
}
List employees=new ArrayList();
而(!emp.name.equals(“STOP”)){
员工emp=新员工();
//从输入读取所有数据
添加(emp);
}
对于(员工emp:员工){
系统输出打印项次(emp.name);
}
我运行时收到“找不到符号”错误。我必须声明列表吗?用代码编辑你的帖子,我看不出来。“System.outprintln(emp.name);“你忘了一个”。“out”和“println”之间的“.”。