我试图将用户周日 - 周六输入的所有小时数传递给3个构造函数中的任何一个。我无法使用我拥有的代码。我做错了什么,我该如何解决? **固定**
public static TimeCard processTimeCard(String data)
{
String[] split = data.split(",");
String employee = split[0];
String project = split[1];
double rate = Double.parseDouble(split[2]);
double hours = 0.0;
String[] days = { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday",
"Friday", "Saturday"};
Scanner keyboard = new Scanner(System.in);
// Get number of hours for each day of the week
for (int index = 0; index < days.length; index++)
{
System.out.println("How many hours on " + days[index] + ".");
hours += Double.parseDouble(keyboard.nextLine());
}
TimeCard arrow = new TimeCard(employee, project, rate, hours);
return arrow;
}
}
class TimeCard
{
// Instance Variables
private String employeeName;
private String project;
private double rate;
private double hours;
//Class Variables
private static int numCards = 0;
private static final double OT_MULTIPLIER = 1.5;
private static final int OT_LIMIT = 40;
/**
* Constructor 1
*/
public TimeCard(String employee, String project, double rate, double hours)
{
this.employeeName = employee;
this.project = project;
this.rate = rate;
this.hours = hours;
numCards++;
}
/**
* Constructor 2
*/
public TimeCard(String employee, String project)
{
rate = 0;
hours = 0.0;
numCards++;
}
/**
* Constructor 3
*/
public TimeCard(String employee)
{
project = "none";
rate = 0;
hours = 0.0;
numCards++;
}
/**
* Accessors
*/
public String getHours()
{
return this.hours;
}
我得到的错误是
error: incompatible types
return this.hours;
required: String
found; double
如何修复此错误
答案 0 :(得分:0)
它无法正常工作,因为它与您的任何构造函数都不匹配。将new TimeCard(employee, project, rate, hours)
与各种构造函数进行比较,您会看到参数不匹配。
更新构造函数#1(或创建一个新构造函数),如下所示:
public TimeCard(String employee, String project, double rate, double hours)
{
this.employeeName = employee;
this.project = project;
this.rate = rate;
this.hours = hours;
numCards++;
}
答案 1 :(得分:0)
我试图将用户周日 - 周六输入的所有小时数传递给3个构造函数中的任何一个
我在你的代码中没有看到它。
我猜你需要通过总小时数。有了这个,你就不会这样做了:
hours = Double.parseDouble(keyboard.nextLine());
你读过它但不做任何事情。您可以尝试添加所有这些小时:
hours += Double.parseDouble(keyboard.nextLine());
阅读完所有小时后,您可以将其传递给构造函数,如下所示:
TimeCard arrow = new TimeCard(employee, project, rate, hours);
但请确保您的主构造函数接受hours
作为参数:
/**
* Constructor 1
*/
public TimeCard(String employee, String project, double rate, double hours)
{
this.employeeName = employee;
this.project = project;
this.rate = rate;
this.hours = hours;
numCards++;
}