我試圖每顯示 10 個數字插入一行,但是當我運行它并輸入 -29 和 29 時,沒有一行顯示。請幫忙,謝謝
int start;
int end;
do
{
cout << "Please enter a number between -30 and 0: ";
cin >>start;
} while(start > 0 || start < -30);
do
{
cout << "Pleaes enter a number between 15 and 30: ";
cin >> end;
} while(end < 15 || end > 30);
for (int i = start; i <= end; i )
{
if (i % 10 == 0)
{
cout << "----------"<<endl;
}
else
cout << start << " " << end <<endl;
}
uj5u.com熱心網友回復:
i增加了兩次。不要在if條件中再次增加它,只需使用以下內容:
if (i % 10 == 0)
uj5u.com熱心網友回復:
正如 Nimish Shah 所說,您要增加i兩次。此外,您正在初始化i等于start,這使得代碼受 的值影響start。嘗試考慮以下代碼:
for (int i = 0; start i <= end; i )
{
cout << start << " " << end <<endl;
if ((i 1) % 10 == 0)
{
cout << "----------"<<endl;
}
}
這樣你就可以在十個數字之后列印一行
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/449387.html
