如何在 C++ 中每显示 10 个数字插入一行?
How to insert a line every 10 numbers displayed in C++?
我试图每显示 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;
}
i
递增两次。不要在 if
条件下再次增加它,只需使用以下内容:
if (i % 10 == 0)
正如 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;
}
}
这样你就可以在十个数字之后打印一行
我试图每显示 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;
}
i
递增两次。不要在 if
条件下再次增加它,只需使用以下内容:
if (i % 10 == 0)
正如 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;
}
}
这样你就可以在十个数字之后打印一行