public class MathUtil { public int f(int a, int b) { if (b == 0) { return a; } else { return f(b, a % b); } }}public class LCMUtil extends MathUtil { @Override public int f(int a, int b) { int m = super.f(a, b); return (a * b) / m; }}public class Main { public static void main(String[] args) { MathUtil mathUtil = new MathUtil(); int gcd = mathUtil.f(12, 18); System.out.println("12和18的最大公约数是:" + gcd); LCMUtil lcmUtil = new LCMUtil(); int lcm = lcmUtil.f(12, 18); System.out.println("12和18的最小公倍数是:" + lcm); }}完整java语句
时间: 2024-01-11 12:03:31 浏览: 197
JAVA实现求最大公约数和最小公倍数
5星 · 资源好评率100%
完整的 Java 语句如下:
```
public class MathUtil {
public int f(int a, int b) {
if (b == 0) {
return a;
} else {
return f(b, a % b);
}
}
}
public class LCMUtil extends MathUtil {
@Override
public int f(int a, int b) {
int m = super.f(a, b);
return (a * b) / m;
}
}
public class Main {
public static void main(String[] args) {
MathUtil mathUtil = new MathUtil();
int gcd = mathUtil.f(12, 18);
System.out.println("12和18的最大公约数是:" + gcd);
LCMUtil lcmUtil = new LCMUtil();
int lcm = lcmUtil.f(12, 18);
System.out.println("12和18的最小公倍数是:" + lcm);
}
}
```
阅读全文