我需要帮助进行手法处理.... [英] i need help for exeption handeling....

查看:76
本文介绍了我需要帮助进行手法处理....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的代码,但是我有一个奇怪的异常.

i write a simple code but i have an strange exception .

int[] vertex;
        int[,] AMatrix;
        int[,] AdjacencyMatrix;
        int Length;

        public void SetVertex()
        {
            Console.WriteLine("Please enter the number of vertices");
            int n = Convert.ToInt32(Console.ReadLine());
            while (n < 2)
            {
                Console.WriteLine("You must enter a number greater than 1\n plz enter another one:");
                n = Convert.ToInt32(Console.ReadLine());
            }
            vertex = new int[n];
            for (int i = 0; i <= n-1; i++)
                vertex[i] = i;
        }
        public void SetEdges()
        {
            AdjacencyMatrix = new int[vertex.Length, vertex.Length];
            for (int i = 0; i < vertex.Length; i++)
                for (int j = 0; j < vertex.Length; j++)
                    AdjacencyMatrix[i, j] = 0;

            Console.WriteLine("please enter the number of the edges:");
            int e = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < e; i++)
            {
                Console.WriteLine("from ");
                int s = 0;
                s = Convert.ToInt32(Console.ReadLine());
                   
                Console.WriteLine("to ");
                int d = 0;
                d = Convert.ToInt32(Console.ReadLine());
                AdjacencyMatrix[s, d] = 1; // <<-------- Exception happens here
            
        }


在我加粗它的行中,我有一个我无法理解的异常,请帮助我删除此异常:(


in line that i bold it i have an exception i cant understand, please help me to remove this exception :(

推荐答案

代码似乎很好,

让我知道您遇到什么错误.

是IndexOutOfRangeException吗?

如果是这样,则您正在为未分配的数组设置一个值.

:)
The code seems to be fine,

Let me know what error you are getting.

Is it IndexOutOfRangeException?

If this is so, then you are setting a value to an array which is not allocated.

:)


由于异常发生在指定的行,我认为用户输入的数字超出了数组的范围.由于有两个值,而您没有给我们提供用户输入的值,因此无法确定哪个是不好的值.

但是,这是最可能的原因.克里斯蒂安(Christian)提到它可能是错误输入的非数字字符,但是在调用Convert.ToInt32的地方会抛出异常.

就其价值而言,您的代码非常糟糕.除了循环控制器以外,绝不要将单字符变量名用于更重要的事情(即使这只是一项家庭作业).我还将使用对象列表,而不是使用双倍下标的整数数组.在命令提示符下使用讲英语的pout,并在提示用户输入有效值时告诉用户什么是有效值.如果您要学习如何编写代码,请学习如何正确编写代码.最后,添加一些注释.
Since the exception happens at the line indicated, I would think that the user entered a digit that was out of range of the array. Since there are two values, and you didn''t give us the values input by the user, it''s impossible to say which is the bad one.

It is, however, the most probably cause. Christian mentioned that it could have been a non-numeric character entered by mistake, but that would have thrown an exception where you called Convert.ToInt32.

For what it''s worth, your code is pretty bad. NEVER use one-character variable names for anything more important than a loop controller (even if this is just a homework assignment). I would also use a List of objects instead of an doubly-subscripted array of integers. Take the text-speak pout of your command prompts, and tell the user what the valid values are when prompting him for them. If you''re going to learn how to write code, learn how to write it correctly. Finally, add some comments.


我想您真的不想要帮助,或者您已经告诉我们例外情况是什么.

I guess you don''t really want help, or you''d have told us what the exception was.

elahe_eli写道:
elahe_eli wrote:

n = Convert.ToInt32(Console.ReadLine());

n = Convert.ToInt32(Console.ReadLine());



如果输入了非数字,这也会引发异常.



This will also throw an exception if a non number is entered.


这篇关于我需要帮助进行手法处理....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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