Posted on 2007-04-14 22:12
停留的風 閱讀(3829)
評論(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 );
}
}
運行結果如圖:
