求两个数的最大公约数:
import javax.swing.JOptionPane;
public class Test10 {
long l_bigNum;
long l_smallNum;
long l_com_divisor;
public Test10() {
// TODO Auto-generated constructor stub
l_bigNum = 0;
l_smallNum = 0;
l_com_divisor = 0;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test10 test = new Test10();
test.getInputValue();
test.calcCommonDivisor();
test.display();
}
public void getInputValue(){
String s_input = JOptionPane.showInputDialog("请输入第一个数");
l_bigNum = Long.parseLong(s_input);
s_input = JOptionPane.showInputDialog("请输入第二个数");
l_smallNum = Long.parseLong(s_input);
makeSureBigNumIsBig();
}
private void makeSureBigNumIsBig(){
if(l_bigNum < l_smallNum){
long l_tmpNum = l_bigNum;
l_bigNum = l_smallNum;
l_smallNum = l_tmpNum;
}
}
public void calcCommonDivisor(){
long l_result = l_smallNum;
long l_bigNumTmp = l_bigNum;
long l_smallNumTmp = l_smallNum;
while(l_bigNumTmp%l_smallNumTmp != 0){
l_result = l_bigNumTmp % l_smallNumTmp;
l_bigNumTmp = l_smallNumTmp;
l_smallNumTmp = l_result;
}
l_com_divisor = l_result;
}
public void display(){
JOptionPane.showMessageDialog(null, l_bigNum+"和"+l_smallNum+"的最大公约数为:"+l_com_divisor);
}
}