想要練習用非遞迴的方式完成這題,請問我為什麼出現的錯誤訊息是輸出短少呢?
請大大們幫忙解惑,謝謝!
import java.util.Scanner;
import java.io.*;
public class java {
public static void main(String[] args) {
int num1, num2;
Scanner scanner = new Scanner(System.in);
num1 = scanner.nextInt();
num2 = scanner.nextInt();
System.out.print(findGCD(num1,num2));
}
public static int toN2(int d, int big){
int cd=1;
if(big % d==0){ //d是n2的因數
cd = d;
}
return cd;
}
public static int findGCD(int num1, int num2){
int gcd=1;
int cd=1;
int small;
int big;
if(num1<=num2){
small=num1;
big=num2;
}else{
small=num2;
big=num1;
}
for(int i=1; i<=small; i++){
if(small % i==0){//找到一個因數
cd=toN2(i,big);
}
if(cd>gcd){
gcd=cd;
}
}
return gcd;
}
}