Java 无法从方法返回值

Java 无法从方法返回值,java,Java,很难返回用户使用以下代码输入的小时数。。。我相信用户从星期日到星期六输入的总小时数已成功传递给第一个构造函数,但我无法返回小时数 public static TimeCard processTimeCard(String data) { String[] split = data.split(","); String employee = split[0]; String project = split[1]; double rate = Double.parseDoub

很难返回用户使用以下代码输入的小时数。。。我相信用户从星期日到星期六输入的总小时数已成功传递给第一个构造函数,但我无法返回小时数

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;
}

如何修复此错误?

查看您的getter方法:

public String getHours()
{
    return this.hours;
}
它希望返回一个
字符串
,但
hours
类型为
double
。因此,请将您的方法更新为:

public double getHours() { /* Your code */ }
但如果您确实需要方法返回
字符串
,则在返回之前,先将
小时
转换为
字符串

return String.valueOf(this.hours);

只需将
double
转换为
String

public String getHours()
{
    return String.valueOf(this.hours);
}   
或者将函数返回类型更改为
double

public double getHours()
{
    return this.hours;
}
更改:

public String getHours()
{
    return this.hours;
}

修改访问者:

public double getHours() {
    return this.hours;
}

伙计,过来,这是什么。在将问题发布到此处之前,请尝试正确调试

  public String getHours()
   {
    return this.hours;
   }

关于这个错误,你还不了解什么<代码>不兼容类型,
必需:字符串
找到:双精度
。这些再清楚不过了。小时数的类型是什么?方法的返回类型是什么?
public double getHours() {
    return this.hours;
}
  public String getHours()
   {
    return this.hours;
   }