【问题标题】:Properly printing a 2D array?正确打印二维数组?
【发布时间】:2013-02-07 22:28:08
【问题描述】:

目前正在为课堂编写 C# 中的康威生活。我一直在采取一些小步骤来学习语言和游戏编程,并且在打印我的 2D 字符数组时遇到了障碍。目前我正在使用 GetLength - 1 来不超过界限,但它无法打印出数组中的最后一个字符。

我的初始文件是什么样的

 +*++
 ++*+
 ****

读入后放入 Char(我相信)

 *  
  * 
****

打印出来的结果

  *

**



using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace ConwaysLife
{
    class Program
    {
        static char[,] universe;
        static void bigBang(int h, int w, List<string> grid)
        {
            universe = new char[h,w];

            int row = 0;

            foreach (string line in grid)
            {

                for (int i = 0; i < line.Length; i++)
                {
                    if (line.ToCharArray()[i] == '*')
                    {
                        universe[row, i] = '*';
                    }
                }
                row++;
            }
        }

         //How I'm attempting to print out my 2D char array
        static void offspring()
        {
            StringBuilder cellLine = new StringBuilder();
            for (int y = 0; y < universe.GetLength(1)-1; y++)
             {
                for (int x = 0; x < universe.GetLength(0)-1; x++)
                {
                    Console.Write(universe[y, x]);
                }
                Console.WriteLine();                
            }
            //pause
            Console.ReadLine();
        }

        static void Main(string[] args)
        {
            List<string> tempLine = new List<string>();

            //Console.WriteLine("File Path?");
            int width = 0;
            int height = 0;

            //read file into List
            using (StreamReader r = new StreamReader("life.txt"))
            {
                while (r.Peek() >= 0)
                {
                    tempLine.Add(r.ReadLine());

                    //compare current width to new width
                    if (tempLine[height].Length >= width) { width = tempLine[height].Length; }

                    //increase height when going to next row
                    height++;
                }
                bigBang(height, width, tempLine);
            }
            offspring();
        }
    }
}

更新后代()

static void offspring()
        {
            StringBuilder cellLine = new StringBuilder();
            for (int x = 0; x <= universe.GetLength(1); x++)
            {
                for (int y = 0; y <= universe.GetLength(0); y++)
                {
                    Console.Write(universe[x, y]);
                }
                Console.WriteLine();
            }
            //pause
            Console.ReadLine();
        }

【问题讨论】:

  • 与你真正的问题无关,但是c#中的字符串有一个索引器,所以不需要使用“ToCharArray”,你可以做line[i]
  • 谢谢。我可能只是在写它,并认为我找到了一条捷径:p猜不出来!

标签: c# multidimensional-array


【解决方案1】:

您的offspring 函数中有一个错误。请注意,您在 bigBang 函数中正确执行此操作。

您正在循环 x &lt; GetLength()-1。你只需要x &lt; GetLength(),因为这不包括x == GetLength()的情况。

类似循环:

for (i = 0; i < 4; i++) 
   Console.WriteLine(i);

输出:

0
1
2
3

【讨论】:

    【解决方案2】:

    我不熟悉游戏原理,但是你的offspring方法有问题。

    y < universe.GetLength(1)-1
    

    这转换为 y &lt; 3 - 1y &lt; 2,使您的迭代从 y = 0 变为 1。

    要修复,只需删除-1 的两个出现。

            for (int y = 0; y < universe.GetLength(1); y++)
            {
                for (int x = 0; x < universe.GetLength(0); x++)
                {
    

    此外,当您访问 universe 时,您的索引会被反转。

    Console.Write(universe[y, x]);
    

    您使用 y 变量访问行,使用 x 访问列。逆向应该是这样的:

    Console.Write(universe[x, y]);
    

    给出最终输出

    ++*
    *+*
    +**
    ++*
    

    【讨论】:

    • 您的建议有效,但打印出来的却是上下颠倒的。我已经用我更新的 offspring() 更新了顶部,但是当正面朝上打印时它仍然不在索引范围内。
    • 你能提供预期的输出吗?我看到您切换了两个 for 循环的顺序,它应该打印与您的输入相同的结构。
    【解决方案3】:

    虽然我将深入研究为什么它没有按预期工作,但我只是在创建数组时将数组的大小传递给我的 offspring() 并在打印时使用这些值。一旦完成了这个小改动,输出就会按预期出现。

     *  
      * 
    ****
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 2015-02-03
      • 2018-12-02
      相关资源
      最近更新 更多