我正在设计一个C++类的程序,我正在使用,教授希望我们制作一个使用“*”输出菱形的程序。我坚持的是我的程序输出太多的行。为什么我的循环执行得太频繁?
而不是
*
***
*
它输出
*
***
*****
***
*
我将如何修改我的代码得到这个工作?我一直在网上寻找答案,整个上午都没有运气。
这里是我的代码:
#include <iostream>
using namespace std;
int main() {
//Define Varaible
int N;
cout << "Please enter a postitive integer: ";
cin >> N;
cout << "Here is your diamond." << endl << endl;
for (int i = 0; i <= 2 * N; i++) {
for (int j = 0; j <= 2 * N; j++) {
if (i <= N) {
if (j < N - i || j > N + i) {
cout << ' ';
}
else {
cout << '*';
}
}
else {
if (j < i - N || j > 3 * N - i) {
cout << ' ';
}
else {
cout << '*';
}
}
}
cout << endl;
}
return 0;
}
有了什么输入,你会得到那些“错误”的图像?如果你得到1行太多并且不理解你自己的代码(...?),添加“N = N-1;”某处。 – usr2564301