【问题标题】:Method not Printing?方法不打印?
【发布时间】:2021-04-26 17:11:46
【问题描述】:

所以我目前正在为我的 C# 类开发一个项目,我们必须使用控制台重新创建扫雷。我对 C# 相当陌生,但之前曾使用过其他代码语言,因此我能够解决大多数问题。我拥有的最新功能是我制作了两个函数(一个创建单元格并将它们存储在列表中,另一个以网格模式打印它们),其中一个可以工作(GenerateCells),但另一个不能运行时输出任何内容。

    //PrintGrid Function
    public static void PrintGrid(int gsize, List<Cell> cells)
    {
        for (int x = 0; x > gsize; x++)
        {
    //testing output
            Console.WriteLine(cells[0].Name);
        }
    }
class Program
    {
        static void Main(string[] args) {
            //Set Default Grid Size
            int gsize = 6;
            //Create List of Cells
            var cells = new List<Cell>
                {
                //Generate "Ghost Cell" preventing exception
                new Cell { Name = "Ghost Cell", XCord = 0, YCord = 0 }
                };
            //Generate Cells for grid
            Cell.GenerateCells(gsize, cells);
            //Print Cells in grid
            Cell.PrintGrid(gsize, cells);
        }
    }

我不确定我做错了什么,因为没有弹出错误。我所能想到的就是我要么把它叫错了,要么把方法设置错了,但我想不通。感谢您提供任何和所有帮助。

【问题讨论】:

  • Console.WriteLine(cells[x].Name)
  • for (int x = 0; x &lt; gsize; x++) { // print at index x },但可能是foreach (var cell in cells) { // print cell.Name or whatever }
  • 仅供参考,List 有一个 Count 属性,因此您无需将其长度作为另一个参数传递

标签: c# methods console.writeline


【解决方案1】:

你只有'>'而不是'

public static void PrintGrid(int gsize, List<Cell> cells)
{
    for (int x = 0; x < gsize; x++)
    {
        //testing output
        //This one has to have the index instead of 0. 
        Console.WriteLine(cells[x].Name);
    }
}

【讨论】:

  • 这不是还在一遍遍地打印单元格[0]吗?
  • 这是故意的。我一直在测试各种东西,但我只需要知道它为什么不打印。不过,我非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2020-04-16
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 2010-10-06
  • 2014-03-13
相关资源
最近更新 更多