Posted on 2007-04-14 22:12
停留的風(fēng) 閱讀(3832)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
Java程序集合
import javax.swing.JOptionPane;
public class GreatestCommonDivistor
{
public static void main(String[] args)
{
//輸入第一個(gè)數(shù)
String strA=JOptionPane.showInputDialog(null,"Enter the first number:","Input First",JOptionPane.QUESTION_MESSAGE);
int numA=Integer.parseInt(strA);
//輸入第二個(gè)數(shù)
String strB=JOptionPane.showInputDialog(null,"Enter the second number:","Input Second",JOptionPane.QUESTION_MESSAGE);
int numB=Integer.parseInt(strB);
//建立一個(gè)歷時(shí)值存儲(chǔ)公約數(shù)
int gcd=1;
for(int k=1;k<=numA&&k<=numB;k++)
if(numA%k==0&&numB%k==0)
gcd=k;
//輸出結(jié)果
System.out.println("The greatest common of the two numbers is "+gcd );
}
}
運(yùn)行結(jié)果如圖:
