将特定数量添加到现有变量VB.net

时间:2016-01-15 19:42:14

标签: vb.net

我正在制作一个无家可归的百万富翁类型的游戏,我必须添加一笔金额" Money"对我剩余的" Money"当我按下按钮+

我的代码:

Public Class Form1
Dim Money As Decimal = 0
Dim Job As String = 0
Dim PlusMoney As Decimal = 0

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Money = +(PlusMoney)
    Label4.Text = (PlusMoney)
    Label6.Text = (Money)

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If Job = 0 And Money = 0 Then
        Label5.Text = "McDonalds csicska"
        PlusMoney = 10

    End If
End Sub
End Class

1 个答案:

答案 0 :(得分:2)

代码的主要问题是Money = +(PlusMoney)应该阅读Money += PlusMoney

Money = +(PlusMoney)相当于Money = PlusMoney,它不会按预期增加PlusMoney的总数,而是在你的情况下将Money设置为PlusMoney(10)。

+=添加了左侧的权利。

在将数字值分配给字符串.ToString()属性

时,数值应添加.Text
    Label4.Text = PlusMoney.ToString()
    Label6.Text = Money.ToString()