试块VB.NET中的算术问题
我对VB.NET和一般编程都比较陌生,所以我还在学习语法的基础知识 我一直收到一个错误,上面写着 预期报表结束 在线试块VB.NET中的算术问题,vb.net,Vb.net,我对VB.NET和一般编程都比较陌生,所以我还在学习语法的基础知识 我一直收到一个错误,上面写着 预期报表结束 在线(b\0+1=1)。我将如何结束此操作以解决错误?谢谢您的回复 b = InputBox("Please Enter Radius.") 'enter radius Try (b\0+1 = 1) Exit Try Catch ex As NullReferenceException Console.WriteLine("Not a whole num
(b\0+1=1)
。我将如何结束此操作以解决错误?谢谢您的回复
b = InputBox("Please Enter Radius.") 'enter radius
Try (b\0+1 = 1)
Exit Try
Catch ex As NullReferenceException
Console.WriteLine("Not a whole number. Please try again")
Console.ReadLine()
Exit Try
End Try
很可能用户输入的不是整数,所以请做好准备。你几乎做到了,但不是很好。你想算出一个非整数。但是非数字输入,比如“abc”呢?在所有情况下,最好假设用户可能输入了错误的输入,并使用逻辑进行检查。您应该使用
If..Else
而不是Try..Catch
来处理该逻辑
我还选择使用Mod
,因为我不知道(b\0+1=1)
应该做什么。\
对数字进行四舍五入,丢弃任何余数,这样对您没有帮助。使用Decimal
数据类型覆盖无限小的余数,即1.00000000000000000000001
(但不能少于。请参阅)。请注意,D
强制执行Mod
运算符的操作数
' I added declaration of 'b' here, assuming it's a string since InputBox returns string.
Dim b As String
b = InputBox("Please Enter Radius.")
Console.WriteLine("You entered {0}", b)
Dim radius As Decimal
If Decimal.TryParse(b, radius) Then ' True if it's a number
If radius Mod 1D = 0D Then ' True if it has no remainder
Console.WriteLine("You entered an integral number. Good job")
Else
Console.WriteLine("Not a whole number. Please try again")
End If
Else
Console.WriteLine("Not even a number. Please try again")
End If
Console.ReadLine()
“预期语句结束”意味着您在一行代码上付出的比预期的要多。在这种情况下,这是因为Try
必须在自己的行上
对于确定用户是否输入了整数的问题,将文本转换为整数的有用方法是and方法。前者在出现错误时抛出异常,而后者返回一个布尔值,指示解析是否成功
“NullReferenceException”不是您想要捕获的内容:您应该查阅文档,了解可以引发异常的方法,看看它可以引发哪些异常。有时捕获任何异常就足够了:
Module Module1
Sub Main()
Dim b As String
Dim isGoodNumber As Boolean = True
Dim radius As Integer
' OPTION ONE: Decline any bad input with a generic error message.
Do
b = InputBox("Please enter the radius as a whole number:")
Console.WriteLine("You entered {0}", b)
Try
radius = Integer.Parse(b)
isGoodNumber = True
Catch ex As Exception
isGoodNumber = False
End Try
If Not isGoodNumber Then
Console.WriteLine("Please enter a whole number.")
End If
Loop Until isGoodNumber
Console.ReadLine()
End Sub
End Module
Module Module1
Sub Main()
Dim b As String
Dim isGoodNumber As Boolean = True
Dim radius As Integer
' OPTION THREE: A shorter way of option one.
Do
b = InputBox("Please enter the radius as a whole number:")
Console.WriteLine("You entered {0}", b)
isGoodNumber = Integer.TryParse(b, radius)
If Not isGoodNumber Then
Console.WriteLine("Please enter a whole number.")
End If
Loop Until isGoodNumber
Console.ReadLine()
End Sub
End Module
如果要根据抛出的错误执行不同的操作,可以使用几个Catch
子句:
Module Module1
Sub Main()
Dim b As String
Dim isGoodNumber As Boolean = True
Dim radius As Integer
' OPTION TWO: Decline any bad input with a more specific error message.
Do
b = InputBox("Please enter the radius as a whole number:")
Console.WriteLine("You entered {0}", b)
Try
radius = Integer.Parse(b)
isGoodNumber = True
Catch ex As ArgumentNullException
isGoodNumber = False
Console.WriteLine("Please enter a whole number.")
Catch ex As FormatException
isGoodNumber = False
Console.WriteLine("Please enter a whole number.")
Catch ex As OverflowException
isGoodNumber = False
Console.WriteLine("Please enter a whole number between {0} and {1}", Integer.MinValue, Integer.MaxValue)
End Try
Loop Until isGoodNumber
Console.ReadLine()
End Sub
End Module
在这种情况下,如果不需要给出错误的输入相关错误消息,甚至不需要抛出异常:
Module Module1
Sub Main()
Dim b As String
Dim isGoodNumber As Boolean = True
Dim radius As Integer
' OPTION ONE: Decline any bad input with a generic error message.
Do
b = InputBox("Please enter the radius as a whole number:")
Console.WriteLine("You entered {0}", b)
Try
radius = Integer.Parse(b)
isGoodNumber = True
Catch ex As Exception
isGoodNumber = False
End Try
If Not isGoodNumber Then
Console.WriteLine("Please enter a whole number.")
End If
Loop Until isGoodNumber
Console.ReadLine()
End Sub
End Module
Module Module1
Sub Main()
Dim b As String
Dim isGoodNumber As Boolean = True
Dim radius As Integer
' OPTION THREE: A shorter way of option one.
Do
b = InputBox("Please enter the radius as a whole number:")
Console.WriteLine("You entered {0}", b)
isGoodNumber = Integer.TryParse(b, radius)
If Not isGoodNumber Then
Console.WriteLine("Please enter a whole number.")
End If
Loop Until isGoodNumber
Console.ReadLine()
End Sub
End Module
Try/Catch定义了一个块——所有的代码都应该放在它里面。你不能指定一个语句来尝试那样做,如果这就是你要做的,如果你把b除以0,你就不能这样做。你到底想做什么?你还需要打开Option Strict。InputBox返回一个字符串,您(显然)正在尝试对其进行数学运算。我不知道你想用
(b\0+1=1)
做什么,但这是错误的。您需要指定结果,但您所应用的任何测试都是错误的。你真的不需要尝试/捕捉在“VB.net Try sample”上进行快速的谷歌搜索,就会发现…samples如果q=Int(q),则
应告诉您它是否为整数q
将是用户输入字符串的解析值