这是我到目前为止的代码,根本没有运行。
Module Average
Sub Main()
Dim v As Integer
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim s As Integer
Console.WriteLine("How Many values are there?")
v = Cint(Console.ReadLine())
If v = 4 Then
Console.WriteLine("Averaging values")
a = CInt(Console.ReadLine())
b = CInt(Console.ReadLine())
c = CInt(Console.ReadLine())
d = CInt(Console.ReadLine())
s = 1
For s = 1 To 1
s = a + b + c + d
s = s / v
Console.Writeline(s)
Else
Console.WriteLine("HI")
Next s
Console.ReadLine()
End If
End Sub
End Module
我上课确实需要这个。如果用户输入了无法输入的数字或需要输入错误,我只是想让它给出错误。
答案 0 :(得分:1)
我真的很想使用Integer.TryParse
进行用户输入,但是我担心添加List类型会被吞噬。
Sub Main()
Dim lstNumbers As New List(Of Integer)
Dim inputNumber As Integer = 0
Console.WriteLine("Welcome to Number Average")
Console.WriteLine("When you are finished entering your numbers, enter Done.")
Do
Console.WriteLine("Enter a number or Done")
Dim input = Console.ReadLine()
If input.ToUpper = "Done".ToUpper Then
Exit Do
End If
lstNumbers.Add(CInt(input))
Loop
If lstNumbers.Count > 0 Then
Dim sum As Integer
Dim s As String
For Each i As Integer In lstNumbers
sum += i
s &= i.ToString & ", "
Next
Dim average As Double = sum / lstNumbers.Count
Console.WriteLine($"The average of the numbers {s} is {average}")
Else
Console.WriteLine("You didn't enter any numbers.")
End If
Console.ReadLine()
End Sub
答案 1 :(得分:0)
如果将代码格式化得更好,则发现问题可能更容易。在For循环中还有一个Else。如果我这样做,似乎可以正常工作(尽管onlinegdb似乎有点小问题):
Sub Main()
Console.WriteLine("Hello World")
Dim v As Integer
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim s As Integer
Console.WriteLine("How Many values are there?")
v = Cint(Console.ReadLine())
If v = 4 Then
Console.WriteLine("Averaging values")
a = CInt(Console.ReadLine())
b = CInt(Console.ReadLine())
c = CInt(Console.ReadLine())
d = CInt(Console.ReadLine())
s = 1
For s = 1 To 1
s = a + b + c + d
s = s / v
Console.Writeline(s)
Next s
Else
Console.WriteLine("HI")
Console.ReadLine()
End If
End Sub
答案 2 :(得分:0)
在C#/ VB.Net中,我们有通用的List
来存储数据集合。在VB.Net中,它看起来像这样:List(Of DataType_Or_Class)
。由于这是一种特殊的Integer
情况,因此您可以按照以下方式简单地创建List
个Integer
:
Dim NumberList as New List(Of Integer)
现在,int16
或int32
甚至是int64
,这是您的选择,但是使用Integer
本身对您的情况没有任何影响。
现在开始检查用户是否输入了数值。这可以通过多种方式实现,例如仅允许数字输入或在提交时读取输入,并检查其是否为正确的整数或,在尝试对所有数字求和时进行异常检查List
中的整数。正如其他答案似乎提到的其他方式一样,这是我提到的第一种方式的示例代码:
Dim _val As String = ""
Console.Write("Enter a number: ")
Dim key As ConsoleKeyInfo
Do
key = Console.ReadKey(True)
If key.Key <> ConsoleKey.Backspace Then
Dim val As Integer = 0
Dim _x As Boolean = Integer.TryParse(key.KeyChar.ToString(), val)
'''Integer.Parse would work aswell, but TryParse will take care of exceptions.
If _x Then
_val += key.KeyChar
Console.Write(key.KeyChar)
End If
Else
If key.Key = ConsoleKey.Backspace AndAlso _val.Length > 0 Then
_val = _val.Substring(0, (_val.Length - 1))
Console.Write(vbBack & " " & vbBack)
End If
End If
Loop While key.Key <> ConsoleKey.Enter
Console.WriteLine() '''Calling Console.WriteLine will execute expected results here.
现在没有更多代码。希望您掌握了基础知识。现在,只需使用简单的逻辑即可。创建一个整数列表,消除除数字键以外的所有不必要的击键/按键操作,按Enter键(While key.Key <> ConsoleKey.Enter
),将每个值添加到列表中,最后计算平均值。
对于最后一步,您可以使用以下代码:
Dim average As Double = If(list.Count > 0, list.Average(),
它将注意列表中没有值。阿迪奥斯,朋友。