gpt4 book ai didi

java - 编译错误 "incompatible types"

转载 作者:行者123 更新时间:2023-12-02 07:09:29 25 4
gpt4 key购买 nike

我收到编译器错误:

Exercise.java:47: error: incompatible types Time 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;
}
}

最佳答案

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

关于java - 编译错误 "incompatible types",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15706007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com