while 循环的最后一步中的 \t 错误 (c++)
error with \t in last step of while loop (c++)
我正在努力完善aligned multiplication table
。一切都很好,但是 \t
在循环的最后一步有点动摇。它插入了两倍的 space 仅用于此步骤,我不知道为什么。
int main()
{
int N;
cin >> N;
int i = 1;
while (i<=N)
{
int j = 1;
while (j<=N)
{
cout << "(" << i << "x";
cout << j << ")";
cout << "=" << i*j;
j++;
cout << "\t\t";
}
i++;
cout << "\n";
}
system("pause");
return 0;
}
输出:
这似乎可以解决您的问题
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int N;
cin >> N;
int i = 1;
while (i<=N)
{
int j = 1;
while (j<=N)
{
cout << "(" << i << "x" << j << ")" << "=" << i*j;
if (i*j < 10) cout << "\t\t";
else cout << "\t";
j++;
}
i++;
cout << endl;
}
system("pause");
return 0;
}
您的问题不仅仅发生在上一次迭代中。它出现在第一个 2 位数字结果之后(例如,尝试 运行 您的 N = 5 代码)。由于结果中多了一个数字(在您的示例中是因为 (4x3)=12),因此要在该行中打印的以下操作被向右推了一个 \t。
我正在努力完善aligned multiplication table
。一切都很好,但是 \t
在循环的最后一步有点动摇。它插入了两倍的 space 仅用于此步骤,我不知道为什么。
int main()
{
int N;
cin >> N;
int i = 1;
while (i<=N)
{
int j = 1;
while (j<=N)
{
cout << "(" << i << "x";
cout << j << ")";
cout << "=" << i*j;
j++;
cout << "\t\t";
}
i++;
cout << "\n";
}
system("pause");
return 0;
}
输出:
这似乎可以解决您的问题
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int N;
cin >> N;
int i = 1;
while (i<=N)
{
int j = 1;
while (j<=N)
{
cout << "(" << i << "x" << j << ")" << "=" << i*j;
if (i*j < 10) cout << "\t\t";
else cout << "\t";
j++;
}
i++;
cout << endl;
}
system("pause");
return 0;
}
您的问题不仅仅发生在上一次迭代中。它出现在第一个 2 位数字结果之后(例如,尝试 运行 您的 N = 5 代码)。由于结果中多了一个数字(在您的示例中是因为 (4x3)=12),因此要在该行中打印的以下操作被向右推了一个 \t。