我正在嘗試編寫能夠在不使用遞歸的情況下找到兩個數字的GCF的代碼(我的老師希望它使用for循環)。我的代碼現在似乎沒有工作,我想知道是否有人可以發現邏輯錯誤。For Loop找到兩個int的GCF
public class Test
{
public static void main(String[] args)
{
System.out.print(findGCF(2, 12));
}
public static int findGCF(int a, int b)
{
int holderGCF = 0;
int greatestItCanBe = 0;
if(a > b)
{
greatestItCanBe = b;
}
else
{
greatestItCanBe = a;
}
for(int i = greatestItCanBe; i > 0; i--)
{
if((a % i == 0) && (b % i == 0))
{
holderGCF = i;
if(i > holderGCF)
{
holderGCF = i;
}
}
}
return holderGCF;
}
}
請編輯你的問題並添加[mcve]。你的問題應該匹配[問],因爲它變得可以回答。 – xenteros