void jButton1_actionPerformed(ActionEvent e) { int a,b,t; int k,r; a=Integer.parseInt(JOptionPane.showInputDialog(this,"输入第一个整数","输入整数",JOptionPane.INFORMATION_MESSAGE)); b=Integer.parseInt(JOptionPane.showInputDialog(this,"输入第二个整数","输入整数",JOptionPane.INFORMATION_MESSAGE)); if (a>b) { k=a;a=b;b=k;}//将两个数交换; k=r=a; t=b; while(k!=0) { k=b%r; b=r; if (k==0) break; else r=k; } System.out.print("the gong yue shu of "+a+" and "+t+" is: "+r+" "); b=a*t/r; System.out.print("the gong bei shu of "+a+ " and " +t+" is: "+b); }展转相除法,先求出最小公约数,然后求出最小公倍数。
{
if((m%i==0)&&(n%i==0))
{
System.out.println("i"+i)
break;
}
}
求两个数,m,n的最大公约数,这个算法比较简单一些
int a,b,t;
int k,r;
a=Integer.parseInt(JOptionPane.showInputDialog(this,"输入第一个整数","输入整数",JOptionPane.INFORMATION_MESSAGE));
b=Integer.parseInt(JOptionPane.showInputDialog(this,"输入第二个整数","输入整数",JOptionPane.INFORMATION_MESSAGE));
if (a>b) { k=a;a=b;b=k;}//将两个数交换;
k=r=a;
t=b;
while(k!=0)
{
k=b%r;
b=r;
if (k==0) break;
else r=k;
}
System.out.print("the gong yue shu of "+a+" and "+t+" is: "+r+" ");
b=a*t/r;
System.out.print("the gong bei shu of "+a+ " and " +t+" is: "+b);
}展转相除法,先求出最小公约数,然后求出最小公倍数。