Visual Basic溢出错误

时间:2016-05-23 02:38:51

标签: vb.net

我正在使用Visual Basic中的程序,需要从文本文件中找到数字数组的平均值。我不断收到错误&#34;算术运算导致溢出&#34;我在"use strict"; var data = [{ "rec": "1", "region": "LEFT", "intrface": "Line-1" }, { "rec": "1", "region": "LEFT", "intrface": "Line-2" }, { "rec": "1", "region": "RIGHT", "intrface": "Line-3" }, { "rec": "1", "region": "RIGHT", "intrface": "Line-4" }]; var s = Snap("#svg"); var height = 40; var canvasWidth = 400; var lineWidth = 180; var rightOffset = canvasWidth / 2 - lineWidth; var leftLines = data.filter(function (line) { return !isRightLine(line); }); var rightLines = data.filter(isRightLine); leftLines.forEach(drawLine); rightLines.forEach(drawLine); var numberOfLines = Math.max(leftLines.length, rightLines.length); var rectSize = 20; var rectangles = []; for (var i = 0; i < numberOfLines; i++) { rectangles.push(drawRect(i)); } function drawLine(data, index) { var intrface = data.intrface; var isRight = isRightLine(data); var x = isRight ? canvasWidth / 2 + rightOffset : 0; var y = height * (index + 1); var stroke = isRight ? 'red' : 'black'; var line = s.line(x, y, x + 180, y); line.attr({ stroke: stroke, strokeWidth: 1 }); var text = s.text(x + 10, y - 5, intrface); text.attr({ fill: stroke, cursor: 'pointer' }); text.click(function () { console.log('clicked', data); //window.location.href = "http://stackoverflow.com/"; }); } // you might want to change this - howeverever you will have to change everywhere in the code that calls isRightLine to pass a primitive vs an object. function isRightLine(_ref) { var region = _ref.region; return region === 'RIGHT'; } function drawRect(index) { var x = canvasWidth / 2 - rectSize / 2; var y = height * (index + 1) - rectSize / 2; var rectangle = s.rect(x, y, rectSize, rectSize); rectangle.attr({ fill: 'black' }); console.log('rr', x, y); return rectangle; } 子中使用counter += 1的部分,并且不知道如何修复它。但我只学习了一种非常基本的VB形式(不是那种先进的)。

到目前为止,这是我的代码:

getNumbers

3 个答案:

答案 0 :(得分:1)

你有一个额外的循环。只需使用EndOfStream函数保持简单。

Sub getNumbers()
   Dim sr As IO.StreamReader
   sr = IO.File.OpenText("digits.txt")
   Dim i As Integer = 0
   While Not sr.EndOfStream
       num(i) = CInt(sr.ReadLine())
       i++
   End While
   sr.Close()
End Sub

答案 1 :(得分:0)

你的问题可能是你没有给计数器一个默认值,但主要是因为在你的计数器循环中你永远不会读到线以移动到下一行。你只是Peek谁不会让你通过文件。请参阅下面的完整解决方案,以完全替换您的代码。

使用Linq查找平均值可能更容易,也是更好的选择。阅读整个答案,以更好地了解Linq。

Console.WriteLine(String.Join(" ", new[] { "aaa", "bbb", "ccc" }));

完整解决方案
以下是使用Linq时代码的样子。您不会需要Dim mean As Double = num.Average() Dim sum As Long = num.Sum() 功能。你应该可以将它粘贴在你的上面并获得你想要的结果。

FindMean

您可以使用Linq做的另一件事是从平均值中排除某些值。以此示例将Public Class Form1 Dim num As List(Of Integer) = New List(Of Integer) Private Sub btnMean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMean.Click getNumbers() lstOutput.Items.Add("The mean is: " & num.Average()) End Sub Sub getNumbers() Dim sr As IO.StreamReader sr = IO.File.OpenText("digits.txt") Do While sr.Peek <> -1 num.Add(CInt(sr.ReadLine)) Loop sr.Close() End Sub End Class 中的9个项目设置为10,然后将第10个项目设置为零。我使用Linq首先过滤大于零的数字,然后执行myNumbers()聚合。

Average

使用类时会更好。考虑这个班级Dim myNumbers(9) As Double For i As Integer = 0 To 8 Step 1 myNumbers(i) = 10 Next i myNumbers(9) = 0 Dim average As Double = myNumbers.Where(Function(num) num > 0).Average() 'Optionally, you could also do it this way: Dim average2 As Double = (From num In myNumbers Where num > 0).Average() MessageBox.Show(average)

Thing

我可以创建该类的集合,Public Class Thing Public Name As String = "" Public Number As Double = 0 Public Sub New (name As String, number As Double) Me.Name= name Me.Number = number End Sub End Class 变得更加强大。在此示例中,我按.Where

平均Thing.Number
Thing.Name

答案 2 :(得分:0)

您是否阅读过documentation for StreamReader.Peek()

  

返回下一个可用字符但不消耗它。

所以这意味着你的循环:

Do While sr.Peek <> -1
  i += 1
Loop

永远不会结束,因为你只是看着缓冲区中的第一个字符并将它留在那里......

解决方案

删除该循环,并将计数器放在第二个循环中。

While Not sr.EndOfStream
    num(i) = CInt(sr.ReadLine())
    i++
End While