【问题标题】:VB ElseIf Statement Limited to 1VB ElseIf 语句限制为 1
【发布时间】:2016-06-12 19:37:25
【问题描述】:

我被大学强迫在 Visual Studio (Basic) 中做一个应用程序,我以前从未使用过这种语言,所以我很困惑。我做了If 声明,它正在工作,然后我添加了EleseIf,它也在工作,但是在我添加第二个ElseIf 之后它不起作用。似乎只有If 语句和第一个ElseIf 对我有用,但我需要的不仅仅是1 个ElseIf 语句。

Public Class receipt
Private Sub receipt_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim outputValue As Decimal = My.Settings.outputamount.Remove(4)
    Dim calculation As Decimal = outputValue * My.Settings.inputamount
    Dim totalwithoutcharge As String = calculation.ToString
    customername.Text = "Name: " + My.Settings.Username
    Label6.Text = "Entered Money: " + My.Settings.inputamount + " " + My.Settings.currency
    Label7.Text = "Converted To: " + totalwithoutcharge + " " + My.Settings.outputcurrency

    If calculation < 100 Then
        Label8.Text = "Charge: 0%"
        Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
    ElseIf calculation > 100 Then
        Label8.Text = "Charge: 1%"
        Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
    ElseIf calculation > 500 Then
        Label8.Text = "Charge: 2%"
        Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
    ElseIf calculation > 1000 Then
        Label8.Text = "Charge: 3%"
        Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
    Else
        Label8.Text = "Something went wrong"
    End If
End Sub 
End Class

我输入了许多值,我已经添加了 600 多个,但它仍然显示“Charge: 1%”,而应该显示 2%。高于 1000 的值也会发生同样的情况,它仍将显示 1%。但是当值低于 100 时就可以了,它显示 0%。

Over 100 works fine too Over 500 doesn't work

我也尝试在两个条件下进行,它会查看范围,但它也不起作用。 我试过 'ElseIf 计算 > 100 和计算 > 500 Then' 但没有区别。

【问题讨论】:

  • 以相反的顺序进行检查。当前 > 100 捕获不小于 100 的所有内容。您的比较可能应该是 >=
  • 因为这是一个逻辑问题,600仍然是> in 100。所以它会继续2语句

标签: vb.net if-statement


【解决方案1】:

首先在集中字符串时使用 & 而不是 +。 其次,如果计算大于 500,它也大于 100、200 等。所以你需要实现一个范围。所以想想逻辑吧。

【讨论】:

  • 谢谢你,我按照 dbasnett 的建议做了,它正在工作。感谢您的宝贵时间,所有“+”现在都替换为“&”。谢谢。
【解决方案2】:

根据我的评论

    If calculation >= 1000 Then

    ElseIf calculation >= 500 Then

    ElseIf calculation >= 100 Then

    Else
        'lass than 100
    End If

【讨论】:

    猜你喜欢
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多