代码代码:输入两个正整数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; } }
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!