您的位置:首页 > 编程语言 > Java开发

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

2014-01-02 20:19 134 查看
public class Maxyue {

    static int yueshu (int a,int b) throws MyException {

        int temp,yue=1;

        if(a<0 || b<0 ) throw new MyException("should be positive");

        if(a>b){

            temp = b;

            b=a;

            a=temp;

        }

        while(yue!=0)

         {

            yue = b%a;

            b=a;

            a=yue;

            

        }

            return b;

        }

    static int beishu(int a,int b) {

        try {

            int yue = yueshu(a,b);

        //int max = a>b?a:b;

        if(yue>1)

        return a*b/yue;

        else return a*b;

        }catch (MyException e){

            return 1;

        }

    }

    

    public static void main(String args[]) {

        int p1=10,p2=15;

        try{

            int pp=yueshu(p1,p2);

            int ppp = beishu(p1,p2);

            System.out.print(pp);

            System.out.print("\n");

            System.out.print(ppp);

        }catch (MyException e) {

            System.out.print(e);

        }

    }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: