[英]How do I return the position of 'findNumber' in the 2-dim array 'matrix'?
我試圖找到用戶輸入的數字的位置(int findNumber)。 我意識到我必須返回一個 Position 類型的實例,但我不知道該怎么做。 我在我的“Main”中創建了一個名為“numberPosition”的實例,但我不能在另一個類中使用它。 這可能是非常基本的東西,但我真的無法弄清楚。 我對編程很陌生,如果問題措辭含糊,我很抱歉。 請放過我吧。
主要的
class Program
{
Random RNG = new Random();
static void Main(string[] args)
{
Program FindNumber = new Program();
FindNumber.Start();
}
void Start()
{
int[,] matrix = new int[8, 10];
InitMatrixRandom(matrix, 0, 100);
DisplayMatrix(matrix);
Console.WriteLine("Give the number to be searched: ");
int findNumber = int.Parse(Console.ReadLine());
Position numberPosition = new Position();
numberPosition.FindNumber(matrix, findNumber);
Console.WriteLine(numberPosition);
Console.ReadKey();
}
void InitMatrixRandom(int[,] matrix, int min, int max)
{
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
matrix[row, col] = RNG.Next(min, max);
}
}
}
void DisplayMatrix(int[,] matrix)
{
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
Console.Write(string.Format("{0,3} ", matrix[row, col]));
}
Console.Write(Environment.NewLine);
}
}
}
班級
class Position
{
public Position numberPosition = new Position();
public Position FindNumber(int[,] matrix, int findNumber)
{
int row = matrix.GetLength(0);
int col = matrix.GetLength(1);
int y, x;
for (y = 0; y < col; y++)
{
for (x = 0; x < row; x++)
{
if (findNumber == matrix[y, x])
{
return numberPosition;
}
}
}
return null;
}
}
您需要為 Position 賦值
class Position
{
public int PositionX {get;set;}
public int PositionY {get;set;}
public Position FindNumber(int[,] matrix, int findNumber)
{
int row = matrix.GetLength(0);
int col = matrix.GetLength(1);
int y, x;
for (y = 0; y < col; y++)
{
for (x = 0; x < row; x++)
{
if (findNumber == matrix[y, x])
{
return new Position { PositionX = x, PositionY = y };
}
}
}
return null;
}
}
如果Position
的目的是存儲x,y
位置,則FindNumber()
函數似乎不應該是該類的一部分。 它可能只需要屬性來存儲x
和y
:
class Position {
public int X;
public int Y;
public Position(int x, int y) {
X = x;
Y = y;
}
public override string ToString()
{
return X + "," + Y;
}
}
您還可以創建一個矩陣類來管理所有這些功能
class Matrix
{
int[,] matrix;
Random RNG = new Random();
public Matrix(int rows, int columns)
{
matrix = new int[rows, columns];
}
void InitMatrixRandom(int min, int max)
{
// Same as before....
}
void DisplayMatrix()
{
// Same as before....
}
void DisplayMatrix(Position numberToHilite)
{
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
if (numberToHilite.X == col && numberToHilite.Y == row) {
Console.ForegroundColor = ConsoleColor.Red;
}
else {
Console.ForegroundColor = ConsoleColor.White;
}
Console.Write(string.Format("{0,3} ", matrix[row, col]));
}
Console.Write(Environment.NewLine);
}
}
public Position FindNumber(int findNumber)
{
for (int y = 0; y < matrix.GetLength(0); y++)
{
for (int x = 0; x < matrix.GetLength(1); x++)
{
if (findNumber == matrix[y, x])
{
// Return a Position object
return new Position(x, y);
}
}
}
return null;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.