简体   繁体   中英

How can I set values in a 2D array from keyboard

I´m working on some codes in C# for school. But there is this exercise that is huge headache.

This is it: I have to develope a code that allows the user to set a value (put in an x) for a 2D array (5x5) from the keyboard. This means that when running the program the user should be able to set one value inside the array, something like "I wana set an "x" in 2,5 and 3,1". I just have no clue how to do that. it´s been already two weeks but i can´t figure it out.

This is what i have so far (updated, thnx to all, specially BradleyDotNET for support):

int[,] data = new int[5, 5]; 

    public void load()
    {
        string[] input = Console.ReadLine().Split('=');
        string[] coordinates = input[0].Split(',');

        int[] intCoordinates = coordinates.Select(s => int.Parse(s)).ToArray();

        data[intCoordinates[0]][intCoordinates[1]] = int.Parse(input[1]);  
    }

    public void view()
    {
        Console.WriteLine("Matrix created is:");
        for (int i = 0; i <= 4; i++)
        {
            Console.Write("\n");
            for (int j = 0; j <= 4; j++)
            {
                Console.Write(data);
            }
        }
        Console.ReadKey();
    }


    static void Main(string[] args)
    {

        Program objeto = new Program();
        objeto.load();
        objeto.view();   

        Console.ReadKey();

        Console.Clear();

I also have to add a feature to let the user add as many "x" to the matriz as he wants, but im planning to do that with a "switch".

So, How do you set values inside the 2d array from keyboard?

Update: The mistake i get here is in line 10, inside "data". It says "Incorrect index number inside []. 2 was expected"

You didn't specify the format the input, so I"ll make one up. If the input was "2,4=10" (meaning set element[2][4] to 10), the code would be:

string[] input = Console.ReadLine().Split('=');
string[] coordinates = input[0].Split(',');

int[] intCoordinates = coordinates.Select(s => int.Parse(s)).ToArray();

matrix[intCoordinates [0]][intCoordinates [1]] = int.Parse(input[1]);

This code has a few problems with it, there is no range validation and if the user enters anything other than an int, it will throw. I'll leave those as an exercise to you, but feel free to ask if you run into trouble.

To explain, we use Console.ReadLine to get a whole line of input. Then we break it on the '=' character to get our coordinates and desired value. We then split the coordinates on ',' to get the different indices.

You can't use strings as array indices, so we call Select to invoke the int.Parse() function on each string, returning us a new array of ints.

Finally, we use the parsed indices to index into matrix and set it to the parsed value from the input.

Something like this should help you.

 public void load()
    {
        for (int i = 0; i <= 4; i++)
        {                
            for (int j = 0; j <= 4; j++)
            {
                Console.WriteLine("enter value for {0},{1}", i, j);
                matrix[i,j]= int.Parse(Console.ReadLine());
            }
        }
    }

BTW, in your view method start the loop from 0 to 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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