我想要一个" x"来自输入框的金额,并使用该值进行值更改,数学做了一些奇怪的事情并且无法正常工作。
Dim infantry As Integer
infantry = InputBox("How many do you want to attack with?", "Choose how many:", , ,)
frmMainGame.lblHPAI.Text = (Val(frmMainGame.lblHPAI.Text) - infantry * 2).ToString("N0")
输入值为1
答案 0 :(得分:0)
Inputbox()返回一个字符串。在将其分配给步兵之前,需要将其转换为整数值。此函数也支持重载,因此您不必包含您不打算使用的参数。
infantry = CInt(InputBox("How many do you want to attack with?", "Choose how many:"))
如果输入的值是非数字,则返回错误。您需要使用try / catch或最好在使用之前验证结果:
Dim infantry As Integer
Dim Result As String = InputBox("How many do you want to attack with?", "Choose how many:")
If IsNumeric(Result) Then infantry = CInt(Result) Else MsgBox("Enter a numeric value", MsgBoxStyle.Critical)
答案 1 :(得分:0)
我们使用Integer.TryParse
来获取输入的值( TryParse ,因为我们无法控制该值是什么)
如果解析成功,我们检索标签值(使用 Parse ,因为该值在我们的控制之下,因此应该始终是有效的int)
最后,我们进行计算并为其表示分配标签。
如果解析失败,我们应该处理(错误消息,循环以获取新值等)
对于(可能两者)解析,你应该注意格式和文化问题,这些问题将决定哪种格式有效。
Dim input = InputBox("How many do you want to attack with?", "Choose how many:")
Dim infantry As Integer
If Integer.TryParse (input, infantry) Then
Dim hpai = Integer.Parse (frmMainGame.lblHPAI.Text, NumberStyles.AllowThousands, CultureInfo.InvariantCulture)
frmMainGame.lblHPAI.Text = (hpai - infantry * 2).ToString("N0")
Else
' handle not an int inputted case
End If