java求三个数的最大公约数和最小公倍数

package yrq.play;
import java.util.Scanner;
public class Text{
private static int x;
private static int y;
private static int z;
public static void main(String [] arrgs){
meun();

}
public  static void meun(){
    System.out.println("您要选择求最大公约数(输入0)还是最小公倍数(输入1)?");
    Scanner scanner = new Scanner(System.in);
    int or=scanner.nextInt();
    if (or==0){
        System.out.println("请选择序号:1.穷举法;2.欧几里得算法;3.辗转相减法");
        int or2=scanner.nextInt();
        if(or2==1){
            addNum();
            jisuan1();
            meun();
        }
        else if(or2==2){
            addNum();
            jisuan2();
            meun();
        }
        else if(or2==3){
            addNum();
            int bigDiv = getBigDiv(x, y);
            int bigDiv1 = getBigDiv(bigDiv, z);
            System.out.println("最大公约数为:"+bigDiv1);
            meun();
        }
        else {
            System.out.println("没有该选项");
            meun();
        }

    }
    else if(or==1){
        System.out.println("请选择序号:1.欧几里得算法;2.穷举法");

        int or3=scanner.nextInt();
        if(or3==1){
            addNum();
            int smallMul = getSmallMul(x, y);
            int smallMul1 = getSmallMul(smallMul, z);
            System.out.println("最小公倍数为"+smallMul1);
            meun();

        }
        else if(or3==2){
            addNum();
            int max=x>y?x:y;
            max=max>z?max:z;
            for (;;max++){
                if(max%x==0&&max%y==0&&max%z==0){
                    System.out.println("最小公倍数为"+max);
                    meun();
                }
            }

        }
        else {
            System.out.println("没有该选项");
            meun();
        }
    }
    else{
        System.out.println("没有此选择");
        meun();
    }
}

public static void addNum(){
    System.out.println("请输入要求的3个数");
    Scanner scanner=new Scanner(System.in);
    x=scanner.nextInt();
    y=scanner.nextInt();
    z=scanner.nextInt();

}
public static int getSmallMul(int a, int b) {// 求两个数的最小公倍数

    return (a * b) / getBigDiv(a, b);
}

public static int getBigDiv(int a, int b) {// 求两个数的最大公约数
    if (b == 0)
        return a;
    return getBigDiv(b, a % b);
}
public static void jisuan1(){
    int j=1;
    for(int i=1;i<=x&&i<=y&&i<=z;i++) {

        if (x % i == 0 && y % i == 0 && z % i == 0) {
            j = i;
        }
    }

    System.out.println("这三个数的最大公约数为:" +j);

}
public static void jisuan2(){
    int j=0;
    while (y != 0) {
        j=x%y;
        x=y;
        y=j;
    }
    j=0;
    while (z!=0){
        j=x%z;
        x=z;
        z=j;
    }
    System.out.println("这三个数的最大公约数为:" +x);

}

}

你可能感兴趣的:(程序设计,java)