import java.util.Scanner;
public class test {
public static void main(String[] srgs) {
Scanner scan=new Scanner(System.in);
System.out.println("请输入第1个数字:");
int n1=scan.nextInt();
System.out.println("请输入第2个数字:");
int n2=scan.nextInt();
int divider=(n1>n2)?n1:n2;
int divisor=(n1>n2)?n2:n1;
int remainder =divisor;
while(remainder!=0) {
divisor=remainder;
remainder=divider/divisor;
divider=divisor;
}
System.out.println("最大公约数为:"+divisor);
scan.close();
}
}
效果测试:
原理:参考这位大神的博客:
https://blog.csdn.net/qq_41575507/article/details/90752742