输出应该如下所示:
########
######
####
##
我通过删除仅有的两个cout语句约束来解决问题:
#include <iostream>
void main(){
int startHash = 8;
for(int row = 1; row <= 4; row++){
for(int hashNum = 1; hashNum <= startHash; hashNum++){
std::cout << "#";
}
std::cout << "\n ";
if(row == 2) std::cout << " ";
if(row == 3) std:: cout << " ";
if(row == 4) std:: cout << " ";
startHash -= 2;
}
system("PAUSE");
}
然而,在这一点上,我无法弄清楚如何在不能输出空间的情况下制作这个形状,因为cout总是从最左边开始并向右移动。任何帮助将不胜感激。
答案 0 :(得分:1)
注意你的ifs中的空格是如何慢慢变长的:
if(row == 2) std::cout << " ";
if(row == 3) std:: cout << " ";
if(row == 4) std:: cout << " ";
似乎那里有模式,看看你是否能找到它。 ; - )
(看到这有大约100%的机会成为家庭作业的问题,为此工作。)
答案 1 :(得分:0)
除了std::cout
#include <iostream>
int main(){
int startHash = 8;
for(int row = 1; row <= 4; row++){
for(int hashNum = 1; hashNum <= startHash; hashNum++){
std::cout << "#";
}
std::cout << "\n ";
if(row == 2) std::clog << " ";
if(row == 3) std::clog << " ";
if(row == 4) std::clog << " ";
startHash -= 2;
}
return 0;
}