Posted on 2007-04-14 22:12
停留的风 阅读(3824)
评论(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 );
}
}
运行结果如图: