简体   繁体   English

在2d Matrix c#top-bottom,left-right中搜索特定位置

[英]Searching for a specific location in a 2d Matrix c# top-bottom, left-right

I'm writing a program to print a 2d matrix array filled with random numbers. 我正在编写一个程序来打印一个填充了随机数的2d矩阵阵列。 The user inputs a number and a method searches for the position in the array from top to bottom and then left to right, but im stuck because im confused as how to return a class i made in a different method to use the location in my main method. 用户输入一个数字,一个方法从上到下,然后从左到右搜索数组中的位置,但我卡住了,因为我很困惑如何返回一个我用不同的方法制作的类来使用我的主要位置方法。

I've now tried to add rows++ and columns++ if the input number is not in that specific row or column and break the loop when the number is found. 我现在尝试添加行++和列++,如果输入的数字不在特定的行或列中,并在找到数字时中断循环。

public class Position
{
    public int row;
    public int column;
}

class Program
{
    static void Main(string[] args)
    {
        Program myProgram = new Program();
        myProgram.Start();
    }

    void Start()
    {
        int[,] matrix = new int[6, 6];
        PrintMatrix(matrix);

        Console.Write("Enter a number you're looking for: ");
        int SearchingNumber = int.Parse(Console.ReadLine());

        SearchingNumber(matrix, SearchingNumber);

        Position loc = new Position();

        Console.WriteLine($"SearchingNumber {SearchingNumber} is located the first time on location: {loc}");
    }

    //Matrix Random number
    void InitMatrixRandom(int[,] matrix, int min, int max)
    {
        Random rnd = new Random();

        for (int r = 0; r < matrix.GetLength(0); r++)
        {
            for (int c = 0; c < matrix.GetLength(1); c++)
            {
                matrix[r, c] = rnd.Next(min, max);
            }
        }
    }

    //Matrix writing
    void PrintMatrix(int[,] matrix)
    {
        InitMatrixRandom(matrix, 1, 100);

        for (int r = 0; r < matrix.GetLength(0); r++)
        {
            for (int c = 0; c < matrix.GetLength(1); c++)
            {
                Console.Write(matrix[r, c] + "\t");
            }
            Console.WriteLine();
        }
    }

    //Matrix positie
    Position SearchingNumber(int[,] matrix, int zoekGetal)
    {
        InitMatrixRandom(matrix, 1, 100);

        Position loc = new Position();

        for (int r = 0; r < matrix.GetLength(0); r++)
        {
            if (r != zoekGetal)
            {
                loc.row++;
            }
            for (int k = 0; k < matrix.GetLength(1); k++)
            {
                if (k != zoekGetal)
                {
                    loc.column++;
                }
                else if (matrix[r, k] == zoekGetal)
                {
                    break;
                }
            }
        }
        return loc;
    }
}

I don't understand what is the need to call InitMatrixRandom(matrix, 1, 100) once again inside SearchingNumber method and PrintMatrix methods? 我不明白在SearchingNumber方法和PrintMatrix方法中再次调用InitMatrixRandom(matrix, 1, 100)的需要是什么?

Just remove the calls- 只需删除电话 -

    void Start() {
    int[, ] matrix = new int[6, 6];

    InitMatrixRandom(matrix, 1, 100); //add here

    PrintMatrix(matrix);

    int SearchingNumberVariable = int.Parse(Console.ReadLine()); //change the name because conflicting with method name

    Position loc = new Position();

    loc = SearchingNumber(matrix, SearchingNumberVariable);

}

//Matrix writing
void PrintMatrix(int[, ] matrix) {
    //InitMatrixRandom(matrix, 1, 100); //remove this

    for (int r = 0; r < matrix.GetLength(0); r++) {
        for (int c = 0; c < matrix.GetLength(1); c++) {
            Console.Write(matrix[r, c] + "\t");
        }
        Console.WriteLine();
    }
}

//Matrix positie
Position SearchingNumber(int[, ] matrix, int zoekGetal) {
    //InitMatrixRandom(matrix, 1, 100); //remove this

    Position loc = new Position();

    for (int r = 0; r < matrix.GetLength(0); r++) {
        if (r != zoekGetal) {
            loc.row++;
        }
        for (int k = 0; k < matrix.GetLength(1); k++) {
            if (k != zoekGetal) {
                loc.collumn++;
            }
            else if (matrix[r, k] == zoekGetal) {
                break;
            }
        }
    }
    return loc;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM