代码代码:输入两个正整数m和n,求其最大公约数和最小公倍数。15 20 5

import java.util.Scanner;

//输入两个正整数m和n,求其最大公约数和最小公倍数。15  20    5 
public class Test {
	public static void main(String[] args) {
		int n = inNumber();
		int m = inNumber();
		int yue = 1;
		int bei = m*n;		
			for (int i = 2; i < n*m; i++) {
				if (m % i == 0 && n % i == 0) {
					yue = yue * i;
					m = m/i;
					n = n/i;
					i=1;
				}
			}		 
		bei = bei / yue;
		System.out.println(yue);
		System.out.println(bei);
	}
	public static int inNumber() {
		Scanner sc = new Scanner(System.in);
		int n = 0;
		System.out.print("请输入整数:");
		while (true) {
			String s = sc.nextLine();
			String regex = "\\d+";
			if (s.matches(regex)) {
				n = Integer.parseInt(s);
				break;
			} else {
				System.out.print("请重新输入整数:");
			}
		}
		return n;
	}
}

 

posted on 2017-03-09 19:30  LoaderMan  阅读(404)  评论(0编辑  收藏  举报

导航