gpt4 book ai didi

vb.net - 检查字符串变量是否具有整数值

转载 作者:行者123 更新时间:2023-12-03 21:01:53 24 4
gpt4 key购买 nike

我正在做一个允许 children 向圣诞老人发送信息的项目。不幸的是,如果他们在 AGE 字段中输入字符串而不是整数,程序会崩溃并返回从字符串“[exampleString]”到类型“Double”的转换无效。
有没有办法检查他们是否输入了整数?这是代码。

If childAge > 0 And childAge < 150 Then
fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
Else
fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"
End If

谢谢,
凯:)

最佳答案

一个非常简单的技巧是 try parse字符串作为整数。如果成功,它是一个整数(惊喜惊喜)。

Dim childAgeAsInt As Integer
If Integer.TryParse(childAge, childAgeAsInt) Then
' childAge successfully parsed as Integer
Else
' childAge is not an Integer
End If

关于vb.net - 检查字符串变量是否具有整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13980538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com