我是一名做Java作业的大学生。我创建了这个程序,允许用户输入工作信息。
问题是我的程序没有返回输入的信息。
我看了一会儿我的节目,但我知道这很简单,我很想念。
public class Employee
{
String name; // Employee name
String employeeNumber; // Employee number
String hireDate; // Employee hire date
int shift; // Employee shift
double payRate;
public void setEmployeeNumber(String e)
{
if (isValidEmpNum(e))
{
employeeNumber = e;
}
else
{
employeeNumber = "";
}
}
public Employee(String name, String e, String hireDate, double payRate, int shift)
{
this.name = name;
this.setEmployeeNumber(e);
this.hireDate = hireDate;
this.payRate = payRate;
this.shift = shift;
}
public Employee()
{
name = "";
employeeNumber = "";
hireDate = "";
}
public void setpayRate(double payRate)
{
this.payRate = payRate;
}
public double getpayRate()
{
return payRate;
}
public void setshift(int shift)
{
this.shift = shift;
}
public int getshift()
{
return shift;
}
public void setName(String name)
{
this.name = name;
}
public void setHireDate(String hireDate)
{
this.hireDate = hireDate;
}
public String getName()
{
return name;
}
public String getEmployeeNumber()
{
return employeeNumber;
}
public String getHireDate()
{
return hireDate;
}
private boolean isValidEmpNum(String e)
{
boolean status = true;
if (e.length() != 5)
status = false;
else
{
if ((!Character.isDigit(e.charAt(0))) ||
(!Character.isDigit(e.charAt(1))) ||
(!Character.isDigit(e.charAt(2))) ||
(e.charAt(3) != '-') ||
(!Character.isLetter(e.charAt(4))) ||
(!(e.charAt(4)>= 'A' && e.charAt(4)<= 'M')))
{
status = false;
}
}
return status;
}
public String toString()
{
String str = "Name: " + name + "\nEmployee Number: ";
if (employeeNumber == "")
{
str += "INVALID EMPLOYEE NUMBER";
}
else
{
str += employeeNumber;
}
str += ("\nHire Date: " + hireDate);
return str;
}
}
我在另一堂课中宣布了这一点。
import javax.swing.JOptionPane;
public class ProductionWorkerDemo extends Employee
{
public static void main(String[] args)
{
String name; // Employee name
String employeeNumber; // Employee number
String hireDate; // Employee hire date
int shift; // Employee shift
double payRate; // Employee pay
String str;
String str2;
name = JOptionPane.showInputDialog("Enter your name: ");
employeeNumber = JOptionPane.showInputDialog("Enter your employee number: ");
hireDate = JOptionPane.showInputDialog("Enter your hire date: ");
str = JOptionPane.showInputDialog("Enter your shift: ");
payRate = Double.parseDouble(str);
str2 = JOptionPane.showInputDialog("Enter your payrate: ");
payRate = Double.parseDouble(str2);
ProductionWorkerDemo pw = new ProductionWorkerDemo();
System.out.println();
System.out.println("Name: " + pw.getName());
System.out.println("Employee Number: " + pw.getEmployeeNumber());
System.out.println("Hire Date: " + pw.getHireDate());
System.out.println("Pay Rate: " + pw.getpayRate());
System.out.println("Shift: " + pw.getshift());
}
}
答案 0 :(得分:3)
您需要使用适当的构造函数或set*
方法来设置对象上的字段。目前,所有这些都是空的,因此get*
方法返回任何内容或默认值。
此外,你不应该使用包含main方法的类扩展Employee
,只需直接使用Employee
类(从类中继承的想法是扩展它,在你的情况下你只需要它作为一个对象,所以保存数据,所以不要从它派生而是使用它):
import javax.swing.JOptionPane;
public class ProductionWorkerDemo
{
public static void main(String[] args)
{
String name; // Employee name
String employeeNumber; // Employee number
String hireDate; // Employee hire date
int shift; // Employee shift
double payRate; // Employee pay
String str;
String str2;
name = JOptionPane.showInputDialog("Enter your name: ");
employeeNumber = JOptionPane.showInputDialog("Enter your employee number: ");
hireDate = JOptionPane.showInputDialog("Enter your hire date: ");
str = JOptionPane.showInputDialog("Enter your shift: ");
payRate = Double.parseDouble(str);
str2 = JOptionPane.showInputDialog("Enter your payrate: ");
payRate = Double.parseDouble(str2);
Employee pw = new Employee(/*provide arguments here*/);
System.out.println();
System.out.println("Name: " + pw.getName());
System.out.println("Employee Number: " + pw.getEmployeeNumber());
System.out.println("Hire Date: " + pw.getHireDate());
System.out.println("Pay Rate: " + pw.getpayRate());
System.out.println("Shift: " + pw.getshift());
}
}
答案 1 :(得分:1)
您只在本地变量上设置员工信息。您没有将它们传递给ProductionWorkerDemo,也不是它的超级员工。
您不需要使用ProductionWorkerDemo扩展Employee,因为ProductionWorkerDemo不是Employee。您可以删除扩展的Employee文本。
您没有将变量传递给Employee。您已经在Employee类中创建了一个构造函数,它将所有构造函数都使用它,以便您可以使用它
Employee pw = new Employee(name, employeeNumber, hireRate, payRate, shift);
现在你会注意到你没有要求转变。
答案 2 :(得分:1)
首先,您需要添加Demo Class的构造函数:
public class ProductionWorkerDemo extends Employee{
public ProductionWorkerDemo(String name, String e, String hireDate, double payRate, nt shift){
{
super(name, e, hireDate, payRate, shift);
}
}
然后在你的班级中你需要实例化:
ProductionWorkerDemo pw = new ProductionWorkerDemo(name,
employeeNumber,
hireDate,
payRate,
shift);
答案 3 :(得分:0)
您在main方法中声明名为name,employeenumber等的变量。当你尝试使用它们时,它会使用那些,而不是你的类变量。
为什么不尝试根据您在Employee类中定义的构造函数制作新的ProductionWorkerDemo?
ProductionWorkerDemo pw = new ProductionWorkerDemo(name,employeeNumber,hireDate,payRate,shift);
而且,您的payRate被分配了两次,您应该更改第一个要转移的,并使用Integer.parseInt
答案 4 :(得分:0)
您在main()
中设置了您正在设置其值的局部变量。然后创建一个ProductionWorkerDemo
对象,它具有相同名称的实例变量,但由于构造函数以这种方式设置,因此最初都是空的。
您永远不会将本地变量传递给ProductionWorkerDemo
对象,因此当您调用getter时,它们会返回空值。
答案 5 :(得分:0)
我解决了我的程序问题,感谢大家的帮助。
我没有将变量传递给Employee。
我将此语句添加到ProductionWorkerDemo类。
员工pw =新员工(姓名,employeeNumber,hireRate,payRate,shift);
P.S。你可以关闭这个帖子。