2016-07-19 7 views
-1

数値と幅の2つの整数を取る単純なプログラムを作成しようとしています。その幅を使って三角形を印刷したいと思う。私は代わりにダブルループを使用する必要があります私の方法で行うことができますか?C#ループからメッセージを印刷できません

class Program 
    { 
     public static int readInt() 
     { 
      int result; 
      string resultString = Console.ReadLine(); 
      result = int.Parse(resultString); 
      return result; 
     } 
     static void Main(string[] args) 
     { 
      int number, width; 

      Console.WriteLine("Enter a number: "); 
      number = readInt(); 

      Console.WriteLine("Enter a width: "); 
      width = readInt(); 

      do { 
       for (int i = width; i < 0; i--) 
       { 
        Console.Write(number); 
        width--; 
        Console.WriteLine(); 
       } 
      } while (width < 0); 

      Console.ReadLine(); 
     } 
    } 

出力: 数:7 幅:4

7777 
777 
77 
7 
+0

まず、幅は正の値なので、この行は 'for(int i = width; i <0; i - ) 'は何もしませんし、それがあったとしても、囲まれた' width - ; 'は幅をゼロにします。第2に、while(width <0)は常に幅がゼロよりも大きいので常に失敗します。コードを最初から再考する必要があります。 – GreatAndPowerfulOz

+0

_print a triangle_とはどういう意味ですか? – Steve

+0

出力がどのように表示されるべきかを教えてください。また、デバッガを使用して、プログラムが実行していることに従い、途中の変数の値を調べます。 –

答えて

0

楽しみのためだけに、このバージョンでは、矩形が

int space = 0; 
do 
{ 
    // Prints initial spaces 
    for(int x = 0; x < space; x++) 
     Console.Write("-"); 

    // Print the number for the current value of width   
    for (int i = 0; i < width; i++) 
     Console.Write(number); 

    // Print final spaces - 
    // Not really needed 
    for (int x = 0; x < space; x++) 
     Console.Write("-"); 

    // Start the new line 
    Console.WriteLine(); 

    //Decrease width by one space for each end (2) 
    width-=2; 

    // Increment the spaces to print before and after 
    space++; 
} while (width > 0); 

ロジック完全に書き直さノートからの部分を中心に印刷しますConsole.WriteLineは改行を追加するので、Console.Writeを使用する必要があります。

関連する問題