如何为以下代码提供三位数输入 [英] How to give three digit input for the following code

查看:112
本文介绍了如何为以下代码提供三位数输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已创建了一个代码来获取矩阵输入,但它不接受三位数输入或两位数输入。



如何根据所需数字输入。



I have created a code to get matrix input but its not accepting three digit input or two digit input.

How to make this get input as per required digit.

Console.WriteLine("\t\tMATRIX TRANSPOSE\n\n");
           try
           {
               Console.Write("Enter Number of Rows: ");
               int nrows = int.Parse(Console.ReadLine());
               Console.Write("\nEnter Number of Columns: ");
               int ncolumns = int.Parse(Console.ReadLine());
           start:
               Console.WriteLine("Enter the elements of the Matrix Seperated by TAB Key   Control+C to Exit\n");
               int[,] input = new int[3, 3];
               try
               {

                   for (int row = 0; row < nrows; row++)
                   {
                       for (int column = 0; column < ncolumns; column++)
                       {
                           var key = Console.ReadKey();
                           if (key.Key != ConsoleKey.Tab && key.Key != ConsoleKey.Enter)
                               input[row, column] = Convert.ToInt32(key.KeyChar.ToString());
                           else
                               column--;
                           if (key.Key == ConsoleKey.Enter)

                               Console.WriteLine("\n\n");

                       }
                   }
               }
               catch (Exception exp)
               {
                   Console.WriteLine(exp.Message);
                   Console.ReadLine();
                   Console.Clear();
                   goto start;
               }
               Console.WriteLine();
               Console.WriteLine("\n\nMatrix Transpose");
               for (int i = 0; i < ncolumns; i++)
               {

                   for (int j = 0; j < nrows; j++)
                   {
                       Console.Write(input[j, i] + "\t");
                   }
                   Console.WriteLine("\n\n");
               }
           }
           catch (Exception exp)
           {
               Console.WriteLine("\n\nEnter Only Numbers");
           }
           Console.ReadLine();

推荐答案

好的,这里有几件事:

1)删除开始标签,和转到 - 然后忘记goto甚至存在了几年。到那时你应该知道为什么这是一个坏主意,何时使用它。在这种情况下,它是完全没有必要的,不应该使用。在我用C#进行编码的这些年里,我从来没有使用过goto - 也许你也不会......



2)你为什么使用如果我输入4行或6列,会导致应用程序崩溃的幻数?

更改此:

OK, there are a few things here:
1) Remove the "start" label, and the "goto" - and then forget that "goto" even exists for a couple of years. By then you should know why it's a bad idea, and when to use it. In this case it is completely unnecessary and should not be used. In all my years of coding in C#, I have never once had to use "goto" - and probably neither will you...

2) Why are you using "magic numbers" that will make your application crash if I enter say 4 rows, or 6 columns?
Change this:
int[,] input = new int[3, 3];



对此:


To this:

int[,] input = new int[nrows, ncolumns];





3)这根本不起作用,是吗?

什么你想要做的就是丢弃ReadKey代码,而只是使用ReadLine:



3) That really doesn't work at all, does it?
What you want to do is throw away the ReadKey code, and just use ReadLine instead:

for (int row = 0; row < nrows; row++)
    {
    string inp = Console.ReadLine();
    string[] values = inp.Split('\t', ',');
    if (values.Length != ncolumns)
        {
        //Report error to user then...
        row--;
        continue;
        }
    for (int column = 0; column < ncolumns; column++)
        {
        int val;
        if (!int.TryParse(values[column], out val))
            {
            // Report error to user, then...
            row--;
            break;
            }
        else
            {
            input[row, column] = val;
            }
        }
    }

接受以逗号或制表符分隔的值,或两者的混合......

That accepts values separated by commas or tabs, or a mixture of the two...


这篇关于如何为以下代码提供三位数输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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