2017-04-11 10 views
1

私はこのコードの各行の前に「ゲーム」を表示しようとしていますが、最後に表示され続けますので、ループを修正する方法を考えることができません正しい時刻に新しい行が作成されます。あなたのコード順序付き配列の書式設定エラーを使用するループ

ここ
Console.WriteLine("Game" + lottoN[x, y] + " "); 
}else { 
Console.Write($"{lottoN[x, y],2}" + " "); 

static void Main() { 

      int[,] lottoNumbers ={ 
            { 4, 7, 19, 23, 28, 36}, 
            {14, 18, 26, 34, 38, 45}, 
            { 8, 10,11, 19, 28, 30}, 
            {15, 17, 19, 24, 43, 44}, 
            {10, 27, 29, 30, 32, 41}, 
            { 9, 13, 26, 32, 37, 43}, 
            { 1, 3, 25, 27, 35, 41}, 
            { 7, 9, 17, 26, 28, 44}, 
            {17, 18, 20, 28, 33, 38} 
           }; 

      int[] drawNumbers = new int[] { 44, 9, 17, 43, 26, 7, 28, 19 }; 

      PrintLottoNumbers(lottoNumbers); 

      ExitProgram(); 
     }//end Main 

static void PrintLottoNumbers(int[,] lottoN) 
     { 
      for (int x = 0; x < lottoN.GetLength(0); x++) { 
       for (int y = 0; y < lottoN.GetLength(1); y++) { 
        if(y < 1 && x > 0) 
        { 
         Console.WriteLine("Game" + lottoN[x, y] + " "); 
        }else { 
         Console.Write($"{lottoN[x, y],2}" + " "); 
         //Console.Write(lottoN[x, y] + " "); 
        } 

       } 
      } 

     }//Print Function For Lotto Numbers 

答えて

1

を望んでいた書式設定でこのお試しください:最もクリーンで読みやすい方法

 for (int x = 0; x < lottoNumbers.GetLength(0); x++) 
     { 
      Console.Write("Game" + lottoNumbers[x, 0] + "\t"); 
      for (int y = 0; y < lottoNumbers.GetLength(1); y++) 
      { 
       Console.Write($"{lottoNumbers[x, y],2}" + "\t"); 
      } 
      Console.WriteLine(); 
     } 
+0

パーフェクト!ありがとう、たくさんの人! – BobFisher3

1

ルックテキスト・ゲーム+ものを書き出すとラインで終了し、そうでない場合は、単に既存のラインに追加のものを書くと述べました。

例えば、多分それはあなたが行の先頭になるようにゲームを必要とする場合は

Game 1 2 3 4 5 game 1 
2 3 4 5 

は、最初の改行を送って示しています!イムは、おそらく

Console.Writeline();  
Console.Write("Game" + lottoN[x, y] + " "); 
}else { 
Console.Write($"{lottoN[x, y],2}" + " "); 

を推測することは、おそらくより多くのあなたが

例えば

game 1 2 3 4 5 
game 1 2 3 4 5 
+0

ここにアクセス!今すぐ最初の番号の後にスペースがあります:3 – BobFisher3

0

をIエントリの行のテキストの作成を個別のメソッドに抽出し、それを各行に対して呼び出します。このような何か:

static void PrintLottoNumbers(int[,] lottoN) 
    { 
     for (int x = 0; x < lottoN.GetLength(0); x++) 
     { 
      Console.WriteLine("Game" + GetRowText(lottoN, x)); 
     } 

    }//Print Function For Lotto Numbers 

    static string GetRowText(int[,] lottoN, int row) 
    { 
     var builder = new StringBuilder(); 
     for (int x = 0; x < lottoN.GetLength(1); x++) 
     { 
      builder.Append(" " + lottoN[row, x]); 
     } 
     return builder.ToString(); 
    } 
1

理由だけではなく、最初のループでは、ゲームの書き込みを移動if-else

 for (int x = 0; x < lottoN.GetLength(0); x++) { 
      Console.Write("\nGame "); 
      for (int y = 0; y < lottoN.GetLength(1); y++) { 
       Console.Write($"{lottoN[x, y],2}"); 
      } 
     } 

で物事を複雑にしています。

追加の空白行が表示されますが、余分な条件を追加することはできません。

Console.Write((x!=0 ? "\n" : string.Empty) + "Game "); 
関連する問題