![](/img/trans.png)
[英]How can I separate a multi-dimension (2D) array by rows into multiple 1D arrays?
[英]How can I override ToString in multi constructor with 2d array?
来自C#初学者的问题,无法在Web中找到解决方案。
这是带有二维数组的构造函数
public MineSweeperBoard(){//新的木板= new BoardItem [MineSweeper_Constants.RowNum,> MineSweeper_Constants.ColNum]; //放置地雷并设置值InitialiseBoard();
}
private BoardItem [,] board;
board.ToString
无法正常工作,它返回类型而不是覆盖它。
public override string ToString()
{
string a = "";
for (int j = 0; j < MineSweeper_Constants.ColNum; j++)
{
for (int i = 0; i < MineSweeper_Constants.RowNum; i++)
{
a += board[i, j].ToString();
}
}
}
这是另一个构造器BoardItem:
public BoardItem(char a)
{
this.itemContents = a;
this.visibleItemContents = ProjectTwo.MineSweeper_Constants.character[10];
}
这里BoardItem.ToString()
正在工作。
public override string ToString()
{
char[] b = new char[1];
b[0] = GetVisibleItemContents();
string a = new string(b);
return a;
}
如何用覆盖ToString打印板子的字符串(我的老师说我必须使用此字符串)?
>{
> class BoardItem
> {
> readonly char itemContents;
> char visibleItemContents;
>
> public BoardItem(char a)
> {
> // 1. The actual contents of the item (use Minesweeper_Constants)
> this.itemContents = a;
>
> // 2. The visible contents of the item (use Minesweeper_Constants) and set to >unknown
> this.visibleItemContents = ProjectTwo.MineSweeper_Constants.character[10];
> }
>
> public char GetItemContents()
> {
> return this.itemContents;
> }
>
> public override string ToString()
> {
> char[] b = new char[1];
> b[0] = GetVisibleItemContents();
> string a = new string(b);
> return a;
>
> }
>
> }
>}
>
另一堂课
enter code here
> class MineSweeperBoard
> {
> private BoardItem [,] board;
> protected static int[,] mineLocation;
> protected static int numberOfMine;
>public MineSweeperBoard()
> {
> // new board
> board = new BoardItem[MineSweeper_Constants.RowNum, >MineSweeper_Constants.ColNum];
> // put mines and set value
> InitialiseBoard();
> }
>public override string ToString()
>{
> string a = "";
> for (int j = 0; j < MineSweeper_Constants.ColNum; j++)
> {
> for (int i = 0; i < MineSweeper_Constants.RowNum; i++)
> {
> a += board[i, j].ToString();
> }
> }
>}
主类:
enter code here
>static void Main(string[] args){
>GameLoop()
>}
>GameLoop(){
MineSweeperBoard board = new MineSweeperBoard();
>Console.WriteLine(board.ToString());
>}
您可以执行以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Board
{
class Program
{
static void Main(string[] args)
{
// Create the board
Board b = new Board();
// Print the maze
Console.Write(b.ToString());
Console.ReadKey();
}
}
class Board
{
private BoardItem[,] items = new BoardItem[10, 10];
// Board contructor - populate the multidimensional array - do you own login here to populate the cells.
public Board()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
items[i, j] = new BoardItem();
}
}
}
// Print each cell from the MineSweeper
public override string ToString()
{
string ret = "";
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
ret += items[i, j].ToString();
}
ret += Environment.NewLine;
}
return ret;
}
}
class BoardItem
{
char BoardItemCharToPriint = '-';
// Print the current cell
public override string ToString()
{
return BoardItemCharToPriint.ToString();
}
}
}
如您所见,我创建了一个包含类的Board类的Board类。 每个类都有其ToString()方法被覆盖。 如果您执行类似的操作,则可以通过重写ToString方法来按老师的要求完成任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.