【发布时间】: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 < gsize; x++) { // print at index x },但可能是foreach (var cell in cells) { // print cell.Name or whatever } -
仅供参考,List 有一个 Count 属性,因此您无需将其长度作为另一个参数传递
标签: c# methods console.writeline