代码如下:
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个整数:");
int num1 = scanner.nextInt();
System.out.print("请输入第二个整数:");
int num2 = scanner.nextInt();
getMaxCommonDivisor(num1, num2);
getMinLcm(num1, num2);
}
private static void getMinLcm(int num1, int num2) {
int min = num1 < num2 ? num2 : num1;
while (true) {
if (min % num1 == 0 && min % num2 == 0) {
System.out.println("最小公倍数:" + min);
break;
}
min++;
}
}
private static void getMaxCommonDivisor(int num1, int num2) {
int max = num1 > num2 ? num2 : num1;
while (true) {
if (num1 % max == 0 && num2 % max == 0) {
System.out.println("最大公约数:" + max);
break;
}
max--;
}
}
}
运行效果图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c021c0514da5a09b8fb0723553b8e141.png)