gpt4 book ai didi

vb.net - 困惑为什么数字之间的比较返回 true

转载 作者:行者123 更新时间:2023-12-04 04:55:31 25 4
gpt4 key购买 nike

我正在使用以下代码来检查我的健康状况是否超过了允许的最大健康状况:

    Public Sub healthCheck()

If Player.CurrentHealth > Player.MaxHealth Then
MsgBox(Player.CurrentHealth & "&" & Player.MaxHealth)
Player.CurrentHealth = Player.MaxHealth
MsgBox(Player.CurrentHealth & "&" & Player.MaxHealth)

ElseIf (0 > Player.CurrentHealth) Then

Player.CurrentHealth = 0

End If

End Sub

我添加了用于调试目的的消息框,它们输出以下字符串:

第一个消息框 = 8&20

第二个消息框 = 20&20

如果第一个告诉我
currenthealth = 8 


maxhealth = 20

为什么它甚至进入 if 结构?
据我所知,8不高于20

编辑:

这似乎只发生在 10 马力以下!所以在 10/20 它不会进入 if 并且只是跳过 if 构造,但是在 9/20 它重置为 20/20(好像比较返回真)

最佳答案

您的健康变量可能是字符串:

MsgBox("8" > "20") // Outputs True

更正
Dim currentHealth As Int32 = Int32.Parse(Player.CurrentHealth)
Dim maxHealth As Int32 = Int32.Parse(Player.MaxHealth)

If currentHealth > maxHealth Then
MsgBox(currentHealth & "&" & maxHealth)
Player.CurrentHealth = PLayer.MaxHealth
currentHealth = maxHealth
MsgBox(currentHealth & "&" & maxHealth)

ElseIf (0 > currentHealth) Then

Player.CurrentHealth = 0

End If

关于vb.net - 困惑为什么数字之间的比较返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16813993/

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