2011116214
gys(int a, int b){
int temp = 0;
if(a<b){
temp=a;
a=b;
b=temp;
}
while(b!=0){
temp=a%b;
a=b;
b=temp;
}
return a;
}
完善后的程序
import java.util.Scanner;
public class test {
public static void main(String[] args) {
int a = 0, b = 0;
System.out.println("请输入第一个数:");
int i = 1;
while (i == 1) {
try {
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
i = 0;
} catch (Exception e) {
System.out.println("请输入规范的的整形数:");
}
}
System.out.println("请输入第二个数:");
while (i == 1) {
try {
Scanner sc = new Scanner(System.in);
b = sc.nextInt();
i = 0;
} catch (Exception e) {
System.out.println("请输入规范的的整形数:");
}
}
int c = gys(a, b);
System.out.println(a + "和" + b + "的最大公约数为:" + c);
}
// 获得最大公约数
static int gys(int a, int b) {
int temp = 0;
if (a < b) {
temp = a;
a = b;
b = temp;
}
while (b != 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
}
编程结果