Как вычислить среднее значение элементов последовательности - Visual Basic .NET
Формулировка задачи:
Вот код. Выводит ошибку: An exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code
Additional information: Последовательность не содержит элементов
В этой строчке кода:
Все работает, если в строчке есть цифры (оценки), но если их нет то ошибка
Листинг программы
- If Not IsNothing(BiologyReadyLine) Then BiologyAverageValue = BiologyReadyLine.Average(Function(n) Convert.ToInt32(n.ToString))
Листинг программы
- Public Class Form1
- Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
- ListView1.Items.Clear()
- TextBox1.Text = WebBrowser1.Document.Body.InnerText
- '####################---Алгебра---####################'
- Dim Line As Integer
- Dim AlgebraPre1, AlgebraPre2, AlgebraPre3, AlgebraReadyLine As String
- Line = TextBox1.Lines.Count - 1
- For x = 0 To Line Step 1
- If TextBox1.Lines(x) = "Предметы" Then
- AlgebraPre1 = TextBox1.Lines(x + 4)
- AlgebraPre2 = Replace(AlgebraPre1, "+", "")
- AlgebraPre3 = Replace(AlgebraPre2, "H", "")
- AlgebraReadyLine = Replace(AlgebraPre3, " ", "")
- End If
- Next
- Dim AlgebraAverageValue As Double
- If Not IsNothing(AlgebraReadyLine) Then AlgebraAverageValue = AlgebraReadyLine.Average(Function(n) Convert.ToInt32(n.ToString))
- Dim AlgebraItem As New ListViewItem("Алгебра")
- AlgebraItem.SubItems.Add(AlgebraReadyLine)
- AlgebraItem.SubItems.Add(Math.Round(AlgebraAverageValue, 2))
- ListView1.Items.Add(AlgebraItem)
- '####################---Английский язык---####################'
- Dim EnglishPre1, EnglishPre2, EnglishPre3, EnglishReadyLine As String
- For x = 0 To Line Step 1
- If TextBox1.Lines(x) = "Предметы" Then
- EnglishPre1 = TextBox1.Lines(x + 5)
- EnglishPre2 = Replace(EnglishPre1, "+", "")
- EnglishPre3 = Replace(EnglishPre2, "H", "")
- EnglishReadyLine = Replace(EnglishPre3, " ", "")
- Label2.Text = EnglishReadyLine & " " & Len(EnglishReadyLine)
- End If
- Next
- Dim EnglishAverageValue As Double
- If Not IsNothing(EnglishReadyLine) Then EnglishAverageValue = EnglishReadyLine.Average(Function(n) Convert.ToInt32(n.ToString))
- Dim EnglishItem As New ListViewItem("Английский")
- EnglishItem.SubItems.Add(EnglishReadyLine)
- EnglishItem.SubItems.Add(Math.Round(EnglishAverageValue, 2))
- ListView1.Items.Add(EnglishItem)
- Label4.Text = AlgebraReadyLine
- '####################---Английский язык---####################'
- Dim BiologyPre1, BiologyPre2, BiologyPre3, BiologyReadyLine As String
- For x = 0 To Line Step 1
- If TextBox1.Lines(x) = "Предметы" Then
- BiologyPre1 = TextBox1.Lines(x + 6)
- BiologyPre2 = Replace(BiologyPre1, "+", "")
- BiologyPre3 = Replace(BiologyPre2, "H", "")
- BiologyReadyLine = Replace(BiologyPre3, " ", "")
- Label2.Text = BiologyReadyLine & " " & Len(BiologyReadyLine)
- End If
- Next
- Dim BiologyAverageValue As Double
- If Not IsNothing(BiologyReadyLine) Then BiologyAverageValue = BiologyReadyLine.Average(Function(n) Convert.ToInt32(n.ToString))
- Dim BiologyItem As New ListViewItem("Биология")
- BiologyItem.SubItems.Add(BiologyReadyLine)
- BiologyItem.SubItems.Add(Math.Round(BiologyAverageValue, 2))
- ListView1.Items.Add(BiologyItem)
- Label4.Text = AlgebraReadyLine
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- WebBrowser1.ScriptErrorsSuppressed = True
- WebBrowser1.Navigate(adress.Text)
- End Sub
- Private Sub WebBrowser1_ProgressChanged(sender As Object, e As WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
- Try
- ProgressBar1.Value = CType((e.CurrentProgress / e.MaximumProgress * 100), Integer)
- Catch Ex As Exception
- End Try
- If ProgressBar1.Value = 100 Then
- Label3.Text = "Успешно!"
- End If
- End Sub
- Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
- Me.Show()
- Me.WindowState = FormWindowState.Normal
- End Sub
- Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
- If Me.WindowState = FormWindowState.Minimized Then
- Me.Hide()
- End If
- End Sub
- Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
- End Sub
- End Class
Решение задачи: «Как вычислить среднее значение элементов последовательности»
textual
Листинг программы
- Dim BiologyAverageValue As Double = 0
- Dim BiologyReadyLine As String = ""
- For x = 0 To Line Step 1
- If TextBox1.Lines(x) = "Предметы" Then
- BiologyReadyLine = TextBox1.Lines(x + 6)
- BiologyReadyLine = Regex.Replace(BiologyReadyLine, "[^\d]", "")
- Label2.Text = BiologyReadyLine & " " & Len(BiologyReadyLine)
- If BiologyReadyLine.Length > 0 Then
- BiologyAverageValue = biology.Average(Function(ch) Char.GetNumericValue(ch))
- End If
- End If
- Next
- Dim BiologyItem As New ListViewItem("Биология")
- BiologyItem.SubItems.Add(BiologyReadyLine)
- BiologyItem.SubItems.Add(Math.Round(BiologyAverageValue, 2))
- ListView1.Items.Add(BiologyItem)
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д