2014-02-23 38 views
3

我無法顯示輸出窗口,有誰能告訴我我在這裏失蹤了什麼?我試圖以金字塔形式顯示#符號。如何使用C#在windows應用程序格式中顯示輸出窗口?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace variable 
{ 
    class Program 
    { 
     static void Main() 
     { 
      for (int row = 0; row < 6; row++) 
      { 
       // Counting backwards here! 
       for (int spaces = 6 - row; spaces > 0; spaces--) 
       { 
        Console.Write(" "); 
       } 

       for (int column = 0; column < (2 * row + 1); column++) 
       { 
        Console.Write("#"); 
       } 
       Console.WriteLine(); 
      } 
      Console.Read(); 
     } 
    } 
} 
+1

什麼問題?怎麼了?您是否在項目屬性中將輸出類型設置爲控制檯? – SLaks

+1

你根本不用'Form'。現在輸出將寫入控制檯('stdout')。 –

回答

-1

刪除Console.Read();

看起來像一個編譯錯誤,一個}後,其工作比其他。你會得到輸出在控制檯

using System.IO; 
using System; 
class Program 
{ 
    static void Main() 
    { 
     // Read in every line in the file. 
     for (int row = 0; row < 6; row++) 
     { 
       // Counting backwards here! 
      for (int spaces = 6 - row; spaces > 0; spaces--) 
      { 
       Console.Write(" "); 
      } 

      for (int column = 0; column < (2 * row + 1); column++) 
      { 
       Console.Write("#"); 

      } 

      Console.WriteLine(); 
     } 
     Console.Read(); 
    } 
} 

輸出:

 # 
    ### 
    ##### 
    ####### 
    ######### 
########### 
1

我假設你已經配置了項目作爲Windows窗體項目。

所以你沒有任何控制檯。

你有三個選擇:

  1. 設置項目類型爲控制檯應用程序。這很可能是不可行的,因爲它需要對應用程序進行太多更改。
  2. 使用P/Invoke附加控制檯。見Show Console in Windows Application?。與選項3相比,優點是您可以在發行版中顯示此控制檯。
  3. 使用Trace.Write/Debug.Write寫入Visual Studio的輸出窗口。
相關問題