【发布时间】:2021-02-28 00:13:41
【问题描述】:
为什么在方法中使用数学函数时返回语句会抛出错误。
public class HCF_1 {
static int hcf(int a, int b)
{
int res = Math.max(a,b);
while(true)
{
if(res%a==0 && res%b==0)
return res;
else res++;
}
return res;
}
public static void main(String[] args) {
System.out.println(hcf(5,25));
}
}
【问题讨论】:
-
无法访问。将第一个
return替换为break。或者删除第二个return。 -
顺便说一下,一旦你修复了错误,按照蓬松的建议,这不会给你 hcf;它会给你 lcm。
-
能否请您善待并发布错误消息?
标签: java