英文:
Can anyone help on this This method must return a result of type int
问题
我被困在这里,我想要打印出这些值,但它就是不允许我。请帮忙。
public int horaPraticada(Intervalo crime) {
if (inicio != crime.getStart() && fim != crime.getEnd()) {
return (crime.getEnd() - crime.getStart());
}
if (inicio > crime.getStart() && fim < crime.getEnd()) {
return (crime.getEnd() - crime.getStart());
}
if (inicio == crime.getStart() && fim > crime.getEnd()) {
return (crime.getEnd() - crime.getStart());
}
if (inicio != crime.getStart() && fim == crime.getEnd()) {
return (crime.getEnd() - crime.getStart());
}
}
英文:
I am stuck in here I want to print these values but it just doesn't allow me. Help please
public int horaPraticada(Intervalo crime) {
if (inicio != crime.getStart() && fim != crime.getEnd()) {
return (crime.getEnd() - crime.getStart()); }
if (inicio > crime.getStart() && fim < crime.getEnd()) {
return (crime.getEnd() - crime.getStart());
}
if (inicio == crime.getStart() && fim > crime.getEnd()) {
return (crime.getEnd() - crime.getStart());
}
if (inicio != crime.getStart() && fim == crime.getEnd()) {
return (crime.getEnd() - crime.getStart());
}
}
答案1
得分: 0
public int horaPraticada(Intervalo crime) {
return (crime.getEnd() - crime.getStart());
}
英文:
Why do not doing this?
public int horaPraticada(Intervalo crime) {
return (crime.getEnd() - crime.getStart());
}
答案2
得分: 0
为了进一步改进GriffeyDog的答案:编译器不知道所有条件是否可能失败。如果它们都失败了,那么该方法将不会返回任何内容。因此,您需要在条件之外添加一个返回语句。
英文:
To further improve GriffeyDog's answer: the compiler does not know, if it is possible or not that all conditions fail. If they would all fail, then the method would not return anything. Therefore, you need to add a return statement outside of the conditions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论