输入两个正整数m和n,求其最大公约数和最小公倍数

package com.itheima_06;

/*
 * 求最大公约数和最小公倍数:
 * 	题目:输入两个正整数m和n,求其最大公约数和最小公倍数
 * 
 * 需求分析:
 * 		利用辗除法.
 */
//最大公约数:
public class maxCommonDivisor1 {
	// 主方法
	public static void main(String[] args) {
		// 调用方法
		commonDivisor(24, 32);
	}

	// 求最大公约数的方法
	static int commonDivisor(int m, int n) {
		// 判断错误的情况
		if (n < 0 || m < 0) {
			System.out.println("ERROR!");
			return -1;
		}
		// 出口
		if (n == 0) {
			System.out.println("the biggest common divisor is :" + m);
			return m;
		}
		// 方法内部调用方法本身
		return commonDivisor(n, m % n);
	}
}

猜你喜欢

转载自blog.csdn.net/guan_moyi/article/details/79901088