package greatestcommondivisor;
import java.util.Scanner;
public class Computer {
static int gcd(int m, int n) {
if (m <= 0 || n <= 0) {
throw new MyException("不能为负整数");
}
return m,n;
if (m < n) {
int k = m;
m = n;
n = k;
}
if (m % n != 0) {
int temp = m % n;
return gcd(n, temp);
}
return n;
}
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int c = gcd(m, n);
System.out.println("最大公约数为:" + c);
scanner.close();
} catch (MyException e) {
System.out.println(e);
}
}
}
下面为:MyException类
package greatestcommondivisor;
public class MyException extends Exception{
public MyException(String ErrorMessage) {
super(ErrorMessage);
}
}