hemir_abap

学习ABAP的小小博客

My Links

Blog Stats

Cached @ 2025/4/26 10:33:25Control ASP.skins_mtclean_controls_blogstats_ascx

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜

Cached @ 2025/4/26 10:33:25Control ASP.skins_mtclean_controls_singlecolumn_ascx

求两个数的最大公约数

求两个数的最大公约数:

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);
 }

}

posted on 2009-04-15 10:59 hemir 阅读(432) 评论(0)  编辑 收藏

Feedback

标题
姓名
主页
内容 
  登录  使用高级评论  Top 订阅回复  取消订阅
[使用Ctrl+Enter键可以直接提交]