初学Java 求最大公约数

 1 import java.util.Scanner;
 2 public class GreatesCommonDivisor {
 3   public static void main(String[] args) {
 4       Scanner input = new Scanner(System.in);
 5       
 6       System.out.print("Enter first integer: ");
 7       int n1 = input.nextInt();
 8       System.out.print("Enter second integer: ");
 9       int n2 = input.nextInt();
10       
11       int gcd = 1;
12       int k = 2;
13       while (k <= n1 && k <= n2) {
14           if (n1 % k == 0 && n2 % k == 0)
15               gcd = k;
16           k++;
17       }
18       
19       System.out.println("The greatest common divisor for " + n1 + " and " + n2 + " is " + gcd);
20   }
21 }
原文地址:https://www.cnblogs.com/leo2li/p/9662841.html