#include
#include
int main() {
using std::cout;
using std::endl;
using std::string;
const int rows = 10;
const string::size_type cols = 5;
for (int r = 0; r != rows; r++){
string::size_type c = 0;
while (c!=cols){
if (r==3||r==6||r==7||r==8){
if (r == 3 || r == 6)
cout << " ";
else
{
if ((r==7&&c==2)||(r==8&&c==1)||(r==8&&c==3))
{
cout << "#";
}
else
{
cout << " ";
}
}
}
else {
cout << "#";
}
++c;
}
}
return 0;
}
运行出来是这
############### ########## # # # #####
可是我想要的是:
#####
#####
#####
到底错在哪了,我为啥只打出行。
在while的外面加一句:cout << endl;就可以了,你没有换行
#include<iostream>
int main() {
using std::cout;
using std::endl;
using std::string;
const int rows = 10;
const string::size_type cols = 5;
for (int r = 0; r != rows; r++) {
string::size_type c = 0;
while (c != cols) {
if (r == 3 || r == 6 || r == 7 || r == 8) {
if (r == 3 || r == 6)
cout << " ";
else
{
if ((r == 7 && c == 2) || (r == 8 && c == 1) || (r == 8 && c == 3))
{
cout << "#";
}
else
{
cout << " ";
}
}
}
else {
cout << "#";
}
++c;
}
cout << endl; //这里换行
}
return 0;
}