【发布时间】:2021-06-04 10:53:39
【问题描述】:
我正在尝试实现一个 try-catch 块来捕获负数和字符作为薪水输入。
我已经在基类、驱动类、子类中尝试过 t-c 块,但无法让它捕捉到这个错误输入。
我尝试使用双变量来确保“工资/收入”输入实际上是错误的。
同样在打印 e1 实例时,第一个字符串值“firstName”总是打印 null。为什么会这样,用调试器检查了一下,它正确读取了值但没有打印出来。
感谢您的帮助。
//FullTimeInstructor.java
package Employee;
public class FullTimeInstructor extends Employee {
private double weeklySalary;
public FullTimeInstructor(String firstName, String lastName, String taxFileNumber, double weeklySalary) {
super(firstName, lastName, taxFileNumber);
this.weeklySalary = weeklySalary;
if (weeklySalary < 0) {
throw new IllegalArgumentException("The salary cannot be a negative or a letter");
} else if (weeklySalary != Double.NaN)
/*tried this also...
If (((Object)weeklySalary).getClass().getName()!="java.lang.Double")*/
{
throw new InputMismatchException("The salary cannot be a letter");
}
}
public double getWeeklySalary() {
return weeklySalary;
}
public void setWeeklySalary(double weeklySalary) {
this.weeklySalary = weeklySalary;
}
@Override
public String toString() {
return firstName + " " + lastName + " " + taxFileNumber + " " + weeklySalary + " " +
Earnings();
}
public double Earnings(){
return weeklySalary;
}
}
//employee.java
package Employee;
abstract class Employee {
public String firstName;
public String lastName;
public String taxFileNumber;
public Employee(String firstName, String lastName,String taxFileNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.taxFileNumber = taxFileNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getTaxFileNumber() {
return taxFileNumber;
}
public void setTaxFileNumber(String taxFileNumber) {
this.taxFileNumber = taxFileNumber;
}
public String toString() {
return firstName + " " + lastName + " " + taxFileNumber;
}
public abstract double Earnings();
}
//EarningsTesting.java
package Employee;
import java.util.InputMismatchException;
public class EarningsTesting {
public static void main(String[] args) throws Exception {
try {
FullTimeInstructor e1 = new FullTimeInstructor("Jane", "Smythe", "456DEF", 29000);
System.out.println(e1);
FullTimeInstructor e2 = new FullTimeInstructor("Janet", "Smith", "987TYR", 20000);
System.out.println(e2);
PartTimeInstructor e3 = new PartTimeInstructor("Alan", "Peters", "345AER", 25.0, 5.0, 20.0, 10.0);
System.out.println(e3);
PartTimeInstructor e4 = new PartTimeInstructor("Boris", "Johnson", "765PER", 25.0, 7.0, 20.0, 11.0);
System.out.println(e4);
SessionalInstructor e5 = new SessionalInstructor("Jeff", "Leyland", "555POI", 20.0, 5.0);
System.out.println(e5);
SessionalInstructor e6 = new SessionalInstructor("Jasmine", "Turner", "856YTE", 20.0, 6.0);
System.out.println(e6);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
} catch (InputMismatchException e) {
System.out.println(e.getMessage());
}
}
}
【问题讨论】:
-
对于您的第二个问题,您的 Employee 构造函数中有一个错字。行'this.lastName = firstName;'应该是 'this.firstName = firstName;'
-
第一个名字问题是因为基类构造函数错误
this.lastName = firstName;应该是this.firstName = firstName; -
您没有对您的
if语句做任何事情?您检查条件,然后立即以;关闭,因此即使条件为真,它也不会执行任何操作。 -
不完全确定你想用这段代码实现什么
if (this.weeklySalary < 0);- 这行没有做任何事情。也许您想在此处抛出一个异常来处理输入为负数的情况?然后在你的测试工具中捕获它?
标签: java