所以我做了一個 while 回圈來查找給定數字的所有除數,如下所示
int a,b;
int temp =0;
cout << "Enter the first number : ";
cin >> a;
cout << "Enter the second number : ";
cin >> b;
int smallestNum = a<b?a:b;
while(true)
{
temp ;
if(a%temp == 0 && b%temp == 0) cout << temp << ", ";
if (temp == smallestNum) break;
}
cout << "are all the divisors for both numbers";
它按預期作業,但是當我嘗試在 for 回圈中執行相同操作時,它沒有按計劃進行
int a,b;
int temp;
cout << "Enter the first number : ";
cin >> a;
cout << "Enter the second number : ";
cin >> b;
temp = a<b?a:b;
for (int i; i == temp; i )
{
if(a%i==0 && b%i==0) cout << i << ", ";
}
cout << "are all the divisors for both numbers";
我什至無法在我的代碼中找到問題來修復它,我該如何修復????
uj5u.com熱心網友回復:
for只要條件是true,回圈就會迭代,您的條件i == temp是(通常)false,您應該將其更改為i != temp
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/352904.html
