Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数组C中的样式化#_C#_Arrays - Fatal编程技术网

C# 数组C中的样式化#

C# 数组C中的样式化#,c#,arrays,C#,Arrays,我创建了一个类似于Tic Tac Toe游戏板的阵列,但我不知道如何解决样式化问题。在前两行中可以,但在最后一行中,每个元素之间没有空间 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ProjectTicTacToe { class Program { sta

我创建了一个类似于Tic Tac Toe游戏板的阵列,但我不知道如何解决样式化问题。在前两行中可以,但在最后一行中,每个元素之间没有空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectTicTacToe
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("   A B C");
            Console.WriteLine();

            string[,] deska = new string[,] {
                { "1  ", "2  ", "3  " },
                { "0 ", "0 ", "0" },
                { "0 ", "0 ", "0" },
                { "0 ", "0 ", "0" },
            };

            for (int j = 0; j < deska.GetLength(1); j++)
            {
                for (int i = 0; i < deska.GetLength(0); i++)
                {
                    Console.Write(deska[i, j]);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间项目
{
班级计划
{
静态void Main(字符串[]参数)
{
控制台写入线(“A B C”);
Console.WriteLine();
字符串[,]deska=新字符串[,]{
{ "1  ", "2  ", "3  " },
{ "0 ", "0 ", "0" },
{ "0 ", "0 ", "0" },
{ "0 ", "0 ", "0" },
};
对于(int j=0;j
它是什么样子的:


谢谢你的帮助

或者尝试如下方式声明数组以快速修复:

  string[,] deska = {
            { "1  ", "2  ", "3  " },
            { "0 ", "0 ", "0 " },
            { "0 ", "0 ", "0 " },
            { "0 ", "0 ", "0 " }
        };
更新:同样在C语言中,您可以灵活地管理控制台输出,请参见下面的小示例。这不是一个通用的解决方案,但您可以按照自己的意愿更好地执行

using System;

namespace ProjectTicTacToe
{
  internal class Program
  {
    private static void Main()
    {
      Console.WriteLine("   A B C");
      Console.WriteLine();

      string[,] deska =
            {
                { "1 ", "2 ", "3 " },
                { "0 ", "0 ", "0" },
                { "0 ", "0 ", "0" },
                { "0 ", "0 ", "0" },
            };

      int prevLenght = 0;
      int currentLenght = 0;
      for (int i = 0; i < deska.GetLength(0); i++)
      {
        currentLenght += prevLenght;
        for (int j = 0; j < deska.GetLength(1); j++)
        {
          Console.SetCursorPosition(currentLenght, Console.CursorTop);
          prevLenght = deska[i, j].Length + 1;
          Console.WriteLine(deska[i, j]);
        }

        Console.CursorTop -= deska.GetLength(1);
      }
      Console.ReadKey();
    }
  }
}
使用系统;
名称空间项目
{
内部课程计划
{
私有静态void Main()
{
控制台写入线(“A B C”);
Console.WriteLine();
字符串[,]deska=
{
{ "1 ", "2 ", "3 " },
{ "0 ", "0 ", "0" },
{ "0 ", "0 ", "0" },
{ "0 ", "0 ", "0" },
};
int prevLenght=0;
int currentLenght=0;
for(int i=0;i
或者尝试这样声明阵列以快速修复:

  string[,] deska = {
            { "1  ", "2  ", "3  " },
            { "0 ", "0 ", "0 " },
            { "0 ", "0 ", "0 " },
            { "0 ", "0 ", "0 " }
        };
更新:同样在C语言中,您可以灵活地管理控制台输出,请参见下面的小示例。这不是一个通用的解决方案,但您可以按照自己的意愿更好地执行

using System;

namespace ProjectTicTacToe
{
  internal class Program
  {
    private static void Main()
    {
      Console.WriteLine("   A B C");
      Console.WriteLine();

      string[,] deska =
            {
                { "1 ", "2 ", "3 " },
                { "0 ", "0 ", "0" },
                { "0 ", "0 ", "0" },
                { "0 ", "0 ", "0" },
            };

      int prevLenght = 0;
      int currentLenght = 0;
      for (int i = 0; i < deska.GetLength(0); i++)
      {
        currentLenght += prevLenght;
        for (int j = 0; j < deska.GetLength(1); j++)
        {
          Console.SetCursorPosition(currentLenght, Console.CursorTop);
          prevLenght = deska[i, j].Length + 1;
          Console.WriteLine(deska[i, j]);
        }

        Console.CursorTop -= deska.GetLength(1);
      }
      Console.ReadKey();
    }
  }
}
使用系统;
名称空间项目
{
内部课程计划
{
私有静态void Main()
{
控制台写入线(“A B C”);
Console.WriteLine();
字符串[,]deska=
{
{ "1 ", "2 ", "3 " },
{ "0 ", "0 ", "0" },
{ "0 ", "0 ", "0" },
{ "0 ", "0 ", "0" },
};
int prevLenght=0;
int currentLenght=0;
for(int i=0;i
让程序为您预先格式化字符串中的布局,而不是尝试预先格式化字符串本身

Console.WriteLine(String.Format("{0,8}{1,3}{2,3}","A","B","C"));
Console.WriteLine();
string[,] deska = new string[,] {
    { "1", "2", "3" },
    { "0", "0", "0" },
    { "0", "0", "0" },
    { "0", "0", "0" },
};

for (int j = 0; j < deska.GetLength(1)-1 ; j++)
{
    Console.WriteLine(String.Format("{0,-5}{1,3}{2,3}{3,3}",deska[0,j],deska[1,j],deska[2,j],deska[3,j]));
}
Console.ReadKey();
Console.WriteLine(String.Format(“{0,8}{1,3}{2,3}”,“A”,“B”,“C”);
Console.WriteLine();
字符串[,]deska=新字符串[,]{
{ "1", "2", "3" },
{ "0", "0", "0" },
{ "0", "0", "0" },
{ "0", "0", "0" },
};
对于(int j=0;j
让程序为您预先格式化字符串中的布局,而不是尝试预先格式化字符串本身

Console.WriteLine(String.Format("{0,8}{1,3}{2,3}","A","B","C"));
Console.WriteLine();
string[,] deska = new string[,] {
    { "1", "2", "3" },
    { "0", "0", "0" },
    { "0", "0", "0" },
    { "0", "0", "0" },
};

for (int j = 0; j < deska.GetLength(1)-1 ; j++)
{
    Console.WriteLine(String.Format("{0,-5}{1,3}{2,3}{3,3}",deska[0,j],deska[1,j],deska[2,j],deska[3,j]));
}
Console.ReadKey();
Console.WriteLine(String.Format(“{0,8}{1,3}{2,3}”,“A”,“B”,“C”);
Console.WriteLine();
字符串[,]deska=新字符串[,]{
{ "1", "2", "3" },
{ "0", "0", "0" },
{ "0", "0", "0" },
{ "0", "0", "0" },
};
对于(int j=0;j
Reverse
deska[i,j]
deska[j,i]
。您的数据被转置,这可能会使遍历和对齐变得笨拙。如果您按行遍历,您会看到最后一个if
0
s集合不包含其他集合所包含的空格。谢谢!请问,您知道如何更改我打印数据的方式吗?像现在一样,第一行应该是1、2、3等等?当我在for循环中切换I和j时,它会显示索引超出数组边界的错误。。。谢谢:)将“1”、“2”和“3”放在左侧,而不是顶部:)@AlešWilk使用GetLength方法时,它会为多维数组返回该维度的长度值。你的价格是3和4。在for循环中,您需要从中减去1,因为您已经将它设置为基于索引编号的循环。GetLength计数从1开始,而不是从0开始。所以,当您运行代码时,它仍然会出错,但在您打印出网格之后,当您切换代码时,它会在有机会打印一行之前出错。因此,无论从哪种角度看,都必须在两个for循环中减去1。将
deska[i,j]
反转为
deska[j,i]
。您的数据被转置,这可能会使遍历和对齐变得笨拙。如果您按行遍历,您会看到最后一个if
0
s集合不包含其他集合所包含的空格。谢谢!请问,您知道如何更改我打印数据的方式吗?像现在一样,第一行应该是1、2、3等等?当我在for循环中切换I和j时,它会显示索引超出数组边界的错误。。。谢谢:)把“1”、“2”和“3”放在左边,不要放在上面