我的个人博客:https://www.wuyizuokan.com
package com.me.main;
import java.util.Scanner;
/**
* 求最大公因数
*/
public class BigCommonFactor
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("please input first number: ");
String s1 = scanner.nextLine();
System.out.println("please input second number: ");
String s2 = scanner.nextLine();
int firstNumber = 0 ;
int secondNumber = 0 ;
try
{
firstNumber = Integer.parseInt(s1);
secondNumber = Integer.parseInt(s2);
}
catch (NumberFormatException e)
{
System.out.println("input have error : firstNumber = " + s1 + ", secondNumber = " + s2 );
System.out.println(e);
throw e;
}
int bigCommonFactor = getBigCommonFactor(firstNumber,secondNumber);
System.out.println("the big common factor is:" + bigCommonFactor);
}
public static int getBigCommonFactor(int n, int m)
{
if (m % n == 0)
{
return n;
}
int r = m % n;
m = n;
n = r;
return getBigCommonFactor(n,m);
}
}