302班

java突击队
posts - 151, comments - 74, trackbacks - 0, articles - 14
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

求两个数的最大公约数(java)

Posted on 2007-04-14 22:12 停留的风 阅读(3819) 评论(0)  编辑  收藏 所属分类: Java程序集合

import javax.swing.JOptionPane;
public class GreatestCommonDivistor

{
 public static void main(String[] args)
 {

  //输入第一个数
  String strA=JOptionPane.showInputDialog(null,"Enter the first number:","Input First",JOptionPane.QUESTION_MESSAGE);
     int numA=Integer.parseInt(strA);

//输入第二个数
  String strB=JOptionPane.showInputDialog(null,"Enter the second number:","Input Second",JOptionPane.QUESTION_MESSAGE);
  int numB=Integer.parseInt(strB);
  

//建立一个历时值存储公约数
  int gcd=1;
  for(int k=1;k<=numA&&k<=numB;k++)
  if(numA%k==0&&numB%k==0)
   gcd=k;

//输出结果
  System.out.println("The greatest common of the two numbers is  "+gcd );
 }
}

运行结果如图:



只有注册用户登录后才能发表评论。


网站导航: