在矩阵内搜索ID的方法-输出问题 [英] A method to search Id inside a matrix -Trouble with output

查看:96
本文介绍了在矩阵内搜索ID的方法-输出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,我正在尝试创建一种允许我在[10,4]矩阵内搜索ID的方法,但是如果不使用嵌套的fors和if and else语句,我将无法做到这一点. .问题与输出有关,我知道结构不正确,但是由于我无能为力,因此我尝试按原样进行制作:

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

                }

            }
        }









    }

}

推荐答案

现在,每次您检查矩阵中的索引时,您都将打印所选的ID不存在".在检查完每个索引之后,您想将该语句移到循环之外.现在,该检查实际上是在说您的ID不在该特定单元格中.我已经对您的代码做了些微改动以反映这一点.我还将您的支票固定在matrix[i,j]而不是matrix[i,0]

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]

还可以使用嵌套的for循环使用.我不相信C#有任何内置的用于搜索多维数组的辅助方法.

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

这篇关于在矩阵内搜索ID的方法-输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆