A method to search Id inside a matrix -Trouble with output

北城以北 提交于 2019-12-25 18:29:42

问题


I am new at programming and I am trying to create a method that allows me search Id inside a [10,4] matrix, however I don't get how to do it without using nested fors and also if and else statement. The problem is related to output, I know the structure isn't correct, but since I don't what else can be done I am trying make it as it is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace menu
{
    class Program
    {
        enum header { id, name, surname, addres };

        public static int id = 1;

        static void Main(string[] args)
        {
            string[,] matrix = new string[10, 4];

            insertStudent(matrix);
            idSearch(matrix);
            Console.ReadKey();
        }


        static int generateId()
        {
            return id++;
        }

        static void insertStudent(string[,] matrix)
        {
            int n = generateId();
            matrix[n - 1, 0] = Convert.ToString(n);

            for (int i = 1; i < matrix.GetLength(1); i++)
            {
                do
                {
                    Console.WriteLine($"Insert {Enum.GetName(typeof(header), i)}");
                    matrix[n - 1, i] = Console.ReadLine();
                }

                while (String.IsNullOrEmpty(matrix[n - 1, i]));

            }
        }

            static void idSearch(string[,] matrix)

        {
            int idChosen=0;
            Console.WriteLine($"Insert ID you want to visualize:");
            int.TryParse(Console.ReadLine(), out idChosen);
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {


                    if (matrix[i, 0] == Convert.ToString(idChosen))
                    {
                        Console.WriteLine(matrix[i, j]);
                    }
                    else
                    {
                        Console.WriteLine("The chosen ID does not exist");
                    }

                }

            }
        }









    }

}

回答1:


Right now you printing "The chosen ID does not exist" every time you check an index in your matrix. You want to move that statement to outside of your loop after you've already checked every index. Right now that check is really saying that your ID is not in that specific cell. I've altered your code slightly to reflect this. I also fixed your check to be on matrix[i,j] instead of matrix[i,0]

Also using a nested for loop is OK to use. I don't believe C# has any built in helper methods for searching multidimensional arrays.

bool found = false;
for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                if (matrix[i, j] == Convert.ToString(idChosen))
                {
                    //note that this will print your id
                    Console.WriteLine(matrix[i, j]);
                    //this would print where it found it
                    Console.WriteLine("Found at [" + i + "," + j + "]");
                    found = true;
                }
            }
        }

if (!found)
{
    Console.WriteLine("The chosen ID does not exist");
}


来源:https://stackoverflow.com/questions/51677285/a-method-to-search-id-inside-a-matrix-trouble-with-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!