C# For循环使用有序数组格式化错误

C# For循环使用有序数组格式化错误,c#,arrays,loops,C#,Arrays,Loops,我试图让我的“游戏”出现在这段代码的每一行的前面,但是它一直出现在最后,我无法解决如何修复我的循环,以便它在正确的时间创建一个新行 static void Main() { int[,] lottoNumbers ={ { 4, 7, 19, 23, 28, 36}, {14, 18, 26, 34, 38, 45},

我试图让我的“游戏”出现在这段代码的每一行的前面,但是它一直出现在最后,我无法解决如何修复我的循环,以便它在正确的时间创建一个新行

 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
static void Main(){
int[,]乐透号码={
{ 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[]drawNumber=新的int[]{44,9,17,43,26,7,28,19};
打印乐透号码(乐透号码);
ExitProgram();
}//端干管
静态void PrintLottoNumbers(int[,]lottoN)
{
for(int x=0;x0)
{
Console.WriteLine(“游戏”+lottoN[x,y]+”);
}否则{
Write($“{lottoN[x,y],2}”+”);
//Console.Write(lottoN[x,y]+“”);
}
}
}
}//乐透号码打印功能
看看你的代码

 Console.WriteLine("Game" + lottoN[x, y] + " ");
 }else {
 Console.Write($"{lottoN[x, y],2}" + " ");
在这里,你说写下文本游戏+内容,并以一行结束,否则,只需在现有的行中写额外的内容

也许它能表现出来

如果你需要游戏在一行的开头,首先发送一个换行!我猜大概

Console.Writeline();     
Console.Write("Game" + lottoN[x, y] + " ");
}else {
Console.Write($"{lottoN[x, y],2}" + " ");
可能更符合您的格式要求

乙二醇

试试这个:

        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();
        }
for(int x=0;x
最干净、可读性最强的方法是将一行条目的文本创建提取到一个单独的方法中,然后为每一行调用该方法。大概是这样的:

    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();
    }
static void PrintLottoNumbers(int[,]lottoN)
{
for(int x=0;x
为什么要用
如果不是

        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}");
            }
         }

快到了!现在它在第一个数字后面有一个空格:3Perfect!谢谢大家!
    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();
    }
        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 ");