请帮助VB.net哪里出错了? [英] please, help with VB.net Where is a mistake?

查看:49
本文介绍了请帮助VB.net哪里出错了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim i, j, n, p As Integer

    n = A.Rows.Count - 1
    For j = 0 To 9
        p = 1
        For i = 0 To 9
            If A(j, i).Value > 0 Then
                p = p * Val(A(j, i).Value)
            End If

        Next i
        A(j, n).Value = p
    Next j
End Sub



任务是写一个程序来查找产品VB.net中10 * 10矩阵中每列的正元素


The task is to write a program to find products of positive elements for each column in a matrix of 10 * 10 in VB.net

推荐答案

首先:
  1. 重命名变量,因此每个变量都是一个正确拼写的英文单词,而不是一个字母。
  2. 使任务成为一个函数。
  3. 摆脱立即常数 9,使其成为一个参数函数。
  4. 注意在循环外定义4个变量的第一行。对于它们中的每一个,确定哪个应该在哪个循环中可见,并使声明更加局部化,因此在不使用它的地方不可见。
  1. Rename you variables so each would be a correctly spelled English word, not a letter.
  2. Make the task a function.
  3. Get rid of immediate constant 9, make it a parameter of that function.
  4. Pay attention for the first line where you define 4 variables outside the loop. For each of them, determine which one is supposed to be visible in what loop, and make the declaration more local, so it would not be visible where it is not used.



这称为重构 http://en.wikipedia.org/wiki/Refactoring [ ^ ]);它不会改变你的功能,但会使你的代码易于管理。



现在在调试器下运行它并调试你的代码。这是你应该学得很好的工作。每次运行时的行为与预期不符时,您将无法提出问题。你需要自己完成这一切,只有这样你才能学到这项工作的重要部分。如果有人告诉你确切的错误,你将失去另一个学习它的机会。每次对运行时都有一点关注,请使用调试器。在问这样的问题之前总是使用它。



除了看这个:你使用函数 Val 。为什么?首先,摆脱VB习惯并尝试使用.NET BCL,方法 System.Int32.Parse System.Int32.TryParse 。一般情况下,避免类似函数Val(ByVal Expression As Object)的非类型化API



更重要的问题是:你为什么解析任何东西?可能是因为您在 A 中存储字符串,而不是数字。这没有道理。由于一些奇怪的原因,这些天初学者最大的谬误之一是使用表示数据而不是数据的字符串。这种趋势非常糟糕且具有破坏性,它无处可带。摆脱它。



这就是现在。



新年祝福!



-SA


This is called refactoring (http://en.wikipedia.org/wiki/Refactoring[^]); it won't change your functionality but will make your code manageable.

Now run it under the debugger and debug your code. This is your work you should learn really well. You won't be able to ask a question each time your runtime behave not as you expected. You need to do it all by yourself, only then you can learn this important part of your work. If someone tells you the exact bug, you will loose yet another chance to learn it. Every time you have a slightest concern on your runtime, use the debugger. And always use it before asking any questions like that.

Besides look at this: you are using the function Val. Why? First of all, get rid of VB habit and try to use .NET BCL, the methods System.Int32.Parse or System.Int32.TryParse. In general, avoid untyped API like Function Val(ByVal Expression As Object) As Double.

More important problem is: why do you parse anything at all? Probably because you store strings in A, not numbers. It makes no sense. By some weird reasons, one of the biggest fallacies of the beginners these days is using strings representing data instead of data. This trend is really bad and destructive, it leads you nowhere. Get rid of it.

That's all for now.

Best wishes in New Year!

—SA


这篇关于请帮助VB.net哪里出错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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