java程序编写 编写程序,接受用户输入的两个整数,求两数的最小公倍数并输出

2025-02-24 04:06:55
推荐回答(1个)
回答1:

import java.util.Scanner;

public class Gongbei {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入第一个数:");
int x = sc.nextInt();
System.out.println("输入第二个数:");
int y = sc.nextInt();
System.out.println("最小公倍数:"+gongbei(x,y));


}
public static int gongyue(int x,int y){//最大公约数
if(x>y){
int t = x;
x = y;
y = t;
}
while(x!=0){
int temp = y%x;
y = x;
x = temp;
}
return y;
}
public static int gongbei(int x,int y){//最小公倍数
int a = x,b = y;
int g = gongyue(a,b);
return x*y/g;
}

}