开发者社区 问答 正文

java最小公倍数和最大公约数

求java程序:求最小公倍数和最大公约数,用java程序写的最小公倍数和最大公约数,急需.

展开
收起
蛮大人123 2016-02-26 15:14:06 2361 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    public class Test{
        public static void main(String[] args){
            int a,b,max;
            Scanner scan = new Scanner(System.in);
            System.out.print("请键入一个整数");
            a = scan.nextInt();
            System.out.print("请再键入一个整数");
            b = scan.nextInt();
            MaxNum mn = new MaxNum();
            max = mn.maxNum(a,b);
            int min = a*b/max;
            System.out.println("最大公约数:" + max);
            System.out.println("最小公倍数:" + min);
        }
    }
    class MaxNum{
        public int maxNum(int x,int y){
            int temp;
            if(x<y){
                temp=x;
                x=y;
                y=temp;
            }
            while(y!=0){
                if(x==y){
                    return x;
                }else{
                    int z = x%y;
                    x=y;
                    y=z;
                }
            }
            return x;
        }
    }
    2019-07-17 18:48:17
    赞同 展开评论