原程序:
import java.util.Scanner;
public class zuidagongyushu {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int a,b,c;
System.out.print("请输入两个正整数:");
a = scan.nextInt();
b = scan.nextInt();
if(a<b){
c=a;
a=b;
b=c;
}
c = a % b;
while(c!=0){
a = b;
b = c;
c = a % b;
}
System.out.print("最大公约数是: " + b);
}
}
改进后的程序:
package suanfa;
import java.util.Scanner;
public class QiuZuiDaGongYueShu {
public static void main(String[] args){
int a,b,c;
Scanner scan=new Scanner(System.in);
System.out.println("请输入两个正整数:");
a=scan.nextInt();
b=scan.nextInt();
if(b>a){
c=a;
a=b;
b=c;
}
while(b!=0)
{
int temp=a%b;
a=b;
b=temp;
}
System.out.println("两个数的最大公约数是"+a);
}
}