0

我收到编译器错误:

练习.java:47:错误:不兼容的类型时间 endTime = startTime.addMinutes(minutes);

                                       ^

必需:找到时间:无效 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;
        }
}     
4

3 回答 3

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;
}
于 2013-03-29T15:20:43.747 回答
0

addMinutes不返回Time( void) 的实例,因此您不能将其分配给endTime

于 2013-03-29T15:18:25.930 回答
0

你有这一行:

Time endTime = startTime.addMinutes(minutes);

但您已声明 addMinutes :

public void addMinutes(int mins)

它不返回任何内容,因此没有返回值分配给 endTime。

于 2013-03-29T15:19:19.317 回答