来自C#初学者的问题,无法在网络中找到解决方案。
这是一个带有2d数组的构造函数
公共MineSweeperBoard() { //新板 board = 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());
>}
答案 0 :(得分:1)
你可以这样做:
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类。每个类都重写了ToString()方法。如果你这样做,你可以通过覆盖ToString方法来完成你老师想要的任务。