Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

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

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");
                    }

                }

            }
        }









    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...