【发布时间】:2016-10-13 11:52:36
【问题描述】:
我正在尝试创建一个当有人键入“.”时结束的 while 循环。
我的代码如下,我得到了代码后面的错误:
Dim x, y As Integer
Dim stuff(x) As String
y = 1
x = 0
While y = 1
x = x + 1
Console.WriteLine("input stuff end with .")
stuff(x - 1) = Console.ReadLine()
If stuff(x - 1) = "." Then
y = 0
End If
End While
错误信息:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in practise.exe
Additional information: Index was outside the bounds of the array.
提前致谢。
【问题讨论】:
-
在增加 x 后立即尝试
ReDim Preserve stuff(x) -
使用列表而不是数组。
-
这是一个示例,说明为什么在项目数量不同时应使用
List等通用集合而不是数组 stackoverflow.com/a/34453165/1383168。
标签: arrays vb.net loops while-loop