【发布时间】:2011-04-21 19:43:19
【问题描述】:
Imports System
Public Class Test
Public Shared Sub Main()
Dim n As Integer
n = Console.ReadLine
Do While n <> 42
System.Console.WriteLine(n)
n = Console.ReadLine
Loop
End Sub
End Class
我收到此代码的运行时错误。我怎样才能改变它。以及如何限制循环打印列表中从 1 到 42 而不是 5 的数字?
【问题讨论】:
-
你能提供你得到的错误吗?
-
我猜你在
n = Console.ReadLine得到编译错误,因为ReadLine返回string -
试试
n = CInt(Console.ReadLine) -
我很确定 CInt 会以同样的方式失败。由于这是一个运行时错误,因此 Option Explicit 必须关闭,并且它在 String 和 Int 之间的隐式转换上失败。因此,CInt 将明确执行转换,但仍应失败。至少,我认为会发生这种情况。如果其他人无法确认或否认,我会在代码中测试。
标签: vb.net