有人能帮忙吗?这个方法必须返回一个整型结果。

huangapple go评论80阅读模式
英文:

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() &amp;&amp; fim != crime.getEnd()) {
	
		return  (crime.getEnd() - crime.getStart()); }
		
	if (inicio &gt; crime.getStart() &amp;&amp; fim &lt; crime.getEnd()) {
		return  (crime.getEnd() - crime.getStart());
	}
	if (inicio == crime.getStart() &amp;&amp; fim &gt; crime.getEnd()) {
		
		return (crime.getEnd() - crime.getStart());
	}
	if (inicio != crime.getStart() &amp;&amp; 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.

huangapple
  • 本文由 发表于 2020年10月20日 03:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64434071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定