2016-11-13 133 views
-4

我是C++中的新手。我想在C++中繪製一個字符V如何繪製V字母形狀在C++中使用星號(*)

我只是做了這個。我不知道如何進一步得到所需的結果。

#include <iostream.h> 
using namespace std; 
int main() 
{ 
int i, j; 
for() 
{ 
    for() 
    { 
     if(i == j) 
     { 
     cout << "*"; 
     } 
     else 
     { 
     cout << " "; 
     } 
} 
cout<< endl; 
return 0; 
} 
+0

這段代碼甚至不應該編譯 - 你的外部for循環沒有右括號,for循環也沒有body(至少需要';;'是必需的) –

+0

我知道。 我只是寫它作爲示例。 –

+0

不要放棄!你超過了一半。如果你正確地寫了'for'頭文件(例如:'for(int i = 0; i <= height; ++ i)'),你會畫出V的左邊。在手邊,你需要爲if語句添加一個或者一個條件(比如'if(i == j ||/* some some maths here/* == j)')。 – Johnsyweb

回答

0

我是下具有不大風扇+ +,但我儘量給邏輯

#include <iostream.h> 

using namespace std; 

int main() { 
    int n; //height of the Char V 
    int i, j; 
    #char s=""; //i know only to adding string if u add s as string 
    for(int x = 1, y = n ; x <= y ; x++, y--) { 
     cout << s; 
     cout << "*"; 
     for(int t = 1 ; t < t ; t++) { 
      cout << ""; 
     } 
     cout << "*"; 
     s=s+""; 
    } 
    cout << endl; 
    return 0; 
} 
1
int main() 
{ 
    std::cout << "*  *\n" 
       " * *\n" 
       " * *\n" 
       " **\n" 
} 
+0

兄弟,尼斯一個 但我要這樣做使用Nexted For Loop –

+0

Brother Student,您可以編輯您的問題以反映完整的要求。 – chicks

0
private static void drawPattern(int height) { 
    // TODO Auto-generated method stub 
    int end=height * 2; 
    int j=0; 
    for (int start=0;start<height;start++,end--) { 
     for (j=0;j<start;j++){ 
      System.out.print(" "); 
     } 
     System.out.print("*"); 
     for (;j<end;j++) { 
      System.out.print(" "); 
     } 
     System.out.println("*"); 
    } 
} 

一個在JAVA髒執行,嘗試將它移植到C++,高度可用戶輸入