【发布时间】:2021-09-08 00:09:57
【问题描述】:
它是一个非常简单的程序,用于向 GCSE 学生展示如何使用 min()、max() 等数组函数,但是当通过 For 循环输入数据时结果不正确 - 当数据属于数组编码。
Dim testscores(10) As Integer
'Dim testscores = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 'min function works on this but not when values are entered using the FOR loop
Dim studentname As String
For x = 0 To 9
Console.WriteLine("this is the x value " & x)
Console.WriteLine("please enter a test score")
testscores(x) = Console.ReadLine()
Next
Console.WriteLine()
For y = 0 To 9
Console.WriteLine("element y is " & y & " " & " the value stored is " & testscores(y))
Next
Array.Sort(testscores)
Console.WriteLine()
For y = 0 To 9
Console.WriteLine("after sorting - element y is " & y & " " & " the value stored is " & testscores(y))
Next`
【问题讨论】:
-
你在这里声明了多少个元素:
Dim testscores(10) As Integer? -- 设置Option Strict ON -
Dim testscores(10) 由于 Integer 创建了一个包含 11 个元素的数组,因此在输入值和打印输出时,您只需迭代前 10 个元素。 docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/…。或者将 for 循环的上限设置为 testcores.Length - 1
-
整数是值类型。值类型具有默认值。 Integer 的默认值为零。数组的第 11 个元素使用 Integer 的默认值填充。
标签: arrays vb.net methods console