gpt4 book ai didi

java - 我需要帮助,我的 java 程序在编译时没有返回任何内容

转载 作者:行者123 更新时间:2023-11-29 08:15:31 24 4
gpt4 key购买 nike

我是一名正在做 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());

}
}

最佳答案

您需要使用适当的构造函数或 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());

}
}

关于java - 我需要帮助,我的 java 程序在编译时没有返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5149037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com