用以下代码很难返回用户输入的小时数...我相信用户从星期日到星期六输入的总小时数已成功传递给第一个构造函数但我无法返回小时。
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 :(得分:1)
看看你的getter方法:
public String getHours()
{
return this.hours;
}
预计会返回String
但hours
类型为double
。因此,请将您的方法更新为:
public double getHours() { /* Your code */ }
但是如果你真的需要你的方法来返回String
,那么在返回之前先将hours
转换为String
:
return String.valueOf(this.hours);
答案 1 :(得分:0)
只需将double
转换为String
public String getHours()
{
return String.valueOf(this.hours);
}
或者将函数返回类型更改为double
public double getHours()
{
return this.hours;
}
答案 2 :(得分:0)
变化:
public String getHours()
{
return this.hours;
}
到
public double getHours()
{
return this.hours;
}
答案 3 :(得分:0)
修改访问者:
public double getHours() {
return this.hours;
}
答案 4 :(得分:0)
伙计来了这个。在此处发布问题之前尝试正确调试。
public String getHours()
{
return this.hours;
}