该程序用于查找 GCD、LCM。当程序到达while循环时我遇到了问题。我的代码如下。
public class GCDLCM {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int a,b;
if(x < y) {
a = y;
b = x;
while((a % b) != 0) {
a = b;
b = a % b;
}
System.out.println("GCD: "+b);
}
else {
a = x;
b = y;
while((a % b) != 0) {
a = b;
b = a % b;
}
System.out.println("GCD: "+b);
}
System.out.println("LCM: "+((x * y)/b));
}
}
错误:线程“主”java.lang.ArithmeticException 中的异常:/在 basicProgrammin.GCDLCM.main(GCDLCM.java:24) 处为零
请帮助我,为什么我的while循环不起作用?提前致谢。