我收到编译错误:
Exercise.java:47:错误:不兼容的类型时间endTime = startTime.addMinutes(分钟);
^
必需:时间 发现:无效 1错误
我尝试使用的方法是:
public void addMinutes(int mins) {
this.mins += mins;
if (this.mins >= 60) { // check if over
addHours(this.mins / 60);
this.mins = this.mins % 60;
}
}
我不确定原因。
import java.util.*;
import java.io.*;
public class Exercise {
private String exercise;
private int minutes;
private Time startTime;
private Time endTime;
private int addedminutes;
public Exercise(String exercisetype, int m, Time start) {
this.exercise = exercisetype;
this.minutes = m;
this.startTime = start;
}
public String getType() {
return this.exercise;
}
public int getMinutes() {
return this.minutes;
}
public Time getStart() {
return this.startTime;
}
public Time getEnd() {
Time endTime = startTime.addMinutes(minutes);
return endTime;
}
public int addMinutes(int added) {
addedminutes = this.minutes + added;
return addedminutes;
}
public Time setStart(Time newstart) {
this.startTime = newstart;
return newstart;
}
public String toString() {
String startStandard = startTime.getStandard();
String endStandard = endTime.getStandard();
String toReturn = (this.exercise + " for " + this.minutes + " minutes," + " from " + startStandard + " to " + endStandard);
return toReturn;
}
public boolean equals(Exercise exTwo) {
return exercise == exTwo.exercise && minutes == exTwo.minutes && startTime == exTwo.startTime;
}
private static String exercisetype;
public static String getTypes() {
String types = ("Exercise types: " + Exercise.exercisetype);
return types;
}
}
答案 0 :(得分:2)
addMinutes
不返回值(void
返回类型)。相反,它会更改调用它的Time对象的状态。
或者,更改方法以在完成后返回时间,例如:
public Time addMinutes(int mins) {
this.mins += mins;
if (this.mins >= 60) { // check if over
addHours(this.mins / 60);
this.mins = this.mins % 60;
}
return this;
}
或者将您的使用情况更改为:
public Time getEnd() {
Time endTime;
startTime.addMinutes(minutes);
//This seems wrong, by the way. startTime will be modified by this call.
endTime = startTime;
return endTime;
}
或者更简单:
public Time getEnd() {
startTime.addMinutes(minutes);
return startTime;
}
答案 1 :(得分:0)
addMinutes
不会返回Time
(void
)的实例,因此您无法将其分配给endTime
答案 2 :(得分:0)
你有这一行:
Time endTime = startTime.addMinutes(minutes);
但您已声明addMinutes:
public void addMinutes(int mins)
哪个不返回任何内容,因此没有返回值分配给endTime。