for循环使用有序数组格式错误

时间:2017-04-11 08:51:15

标签: c# arrays loops

我试图让我的'游戏'出现在这段代码的每一行的前面,但它一直出现在最后,我无法弄清楚如何修复我的循环,以便它在正确的时间。

 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

4 个答案:

答案 0 :(得分:1)

查看您的代码

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

在这里你说过写出文字游戏+东西并用一条线终止,否则,只需在现有线上写下额外的东西。

例如,它可能显示

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

答案 1 :(得分: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();
        }

答案 2 :(得分: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 "); 

答案 3 :(得分:0)

最干净,最易读的方法是将一行条目的文本创建提取到一个单独的方法中,然后为每一行调用该方法。像这样:

    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();
    }
相关问题