这个练习题的for循环怎么写?

光若无影 2016-06-06 01:15:38


 string[,] myIntArray2;
myIntArray2 = new string[,] {
{ "兵", "+", "+", "将", "+", "军", "军", "+", "+"},
{ "+", "+", "+", "+", " 兵", "+", "+", "+", "+"},
{ "兵", "+", "+", "+", "+", "+", "+", "+", "+"},
{ "+", "+", "+", "+", "+", "+", "+", "+", "+"},
{ "+", "+", "+", "+", "+", "+", "+", "+", "+"},
{ "+", "+", "+", "+", "+", "卒", "相", "+", "+"},
{ "+", "+", "+", "+", "兵", "+", "+", "+", "+"},
{ "炮", "+", "卒", "+", "相", "马", "+", "+", "+"},
{ "+", "+", "+", "+", "卒", "+", "+", "+", "+"},
{ "+", "+", "+", "+", "+", "+", "炮", "炮", "车"},
};

for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 10; j++)
{
Console.WriteLine(myIntArray2[i, j]);
}

}


不知道怎么写了,刚学到数组这节
...全文
160 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
光若无影 2016-06-06
  • 打赏
  • 举报
回复
 class Program
    {
        static void Main(string[] args)
        {
            string[,] myIntArray2;
            myIntArray2 = new string[,] {
                                          { "兵", "+", "+", "将", "+", "军", "军", "+", "+"},
                                          { "+", "+", "+", "+", " 兵", "+", "+", "+", "+"},
                                          { "兵", "+", "+", "+", "+", "+", "+", "+", "+"},
                                          { "+", "+", "+", "+", "+", "+", "+", "+", "+"},
                                          { "+", "+", "+", "+", "+", "+", "+", "+", "+"},
                                          { "+", "+", "+", "+", "+", "卒", "相", "+", "+"},
                                          { "+", "+", "+", "+", "兵", "+", "+", "+", "+"},
                                          { "炮", "+", "卒", "+", "相", "马", "+", "+", "+"},
                                          { "+", "+", "+", "+", "卒", "+", "+", "+", "+"},
                                          { "+", "+", "+", "+", "+", "+", "炮", "炮", "车"},       
                                         };

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Console.Write(myIntArray2[i, j]);
                }
                Console.WriteLine("");
            }
            Console.ReadKey();

        }
    }
好了,谢谢大神们帮解答~
xuzuning 2016-06-06
  • 打赏
  • 举报
回复
19 行 Console.Write(myIntArray2[i, j]); 21 行 Console.WriteLine("");
  • 打赏
  • 举报
回复
搞错了,其实没必要EmptyPosition,直接设置ChessPosition,然后初始化数组ChessPosition[,],没设置具体ChessPosition的就是空格位置,x,y本身也没必要,直接依赖二位数组来输出
  • 打赏
  • 举报
回复
class EmptyPosition
{
      public int X{ get;set;}
      public int Y {get;set;}
}
class ChessPosition:EmptyPosition
{

      public int Text{get;set;}
      public Color Color {get;set;}
}

EmptyPosition[,] positions = new EmptyPosition[10,9];
//xxxx
当然你也可以Empty和正常的棋子没区别,都具备Text和Color属性,这样输出时就少个判断棋子类型的过程

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧