2016-11-14 80 views
-6

我是一名初学者,我必须从#中打印字母“N”。 到目前为止,我只能打印| \,所以我仍然错过了最后一个'腿'。 我真的不知道我是如何得到这个..如果任何人都可以帮助我解释! 这里是我的代码:用#打印大写字母N#

#include <iostream> 
using namespace std; 

int main() 
{ 

    int i, j; 

    for (i = 1; i <= 9; i++) 
    { 
     cout << "#"; 
     for (j = 1; j <= 12; j++) 
     { 
      if (i == j) 
      { 
       cout << "#"; 
      } 
      else 
      { 
       cout << " "; 
      } 
     } 

     cout << endl; 
    } 

    return 0; 
} 
+0

您需要在每行的末尾打印一个'#'。如果你看看你的代码,你应该找到你结束行的地方。你只需要改变它。 – NathanOliver

回答

4
for (i = 1; i <= 9; i++) //prints one line at a time 
{ 
    cout << "#"; 
    for (j = 1; j <= 9; j++) 
    { 
     if (i == j) 
     { 
      cout << "#"; //Diagonal part 
     } 
     else 
     { 
      cout << " "; 
     } 
    } 
    cout << "#"; // <<< You missed this 

    cout << endl; 
} 

小更优雅(只使用一个for -loop):

for (i = 1; i <= 9; i++) 
{ 
    string s = "#"; 
    s.append(i-1, ' '); 
    s +='#'; 
    s.append(9-i, ' '); 
    s +='#'; 
    cout << s << endl; 
} 
1

我会去的 “Cheeting” 的方式打印确切的事情,而不重整与循环。

cout << "##  #" << endl 
cout << "# #  #" << endl 
cout << "# # #" << endl 
cout << "# # #" << endl 
cout << "# # #" << endl 
cout << "#  # #" << endl 
cout << "#  ##" << endl 

容易馅饼。

+0

你让我的一天 – Treycos

-1
for(int y=0; y<9;y++){ 

    for(int i=0; i<9; i++){ 

    if((i==8&&y==0) or(i==8&&y==8)){std::cout<<" ";} 

    if(i==0 or i==8){std::cout<<"#";}else{std::cout<<" ";}; 

    if(i>0 && i<8){if(i==y){std::cout<<"#";std::cout<<" ";}else{std::cout<<" ";};}; 

    };std::cout<<"\n";}; 
+0

完美的对角线:D –