Visual Basic If..Then..Else String语句

时间:2015-03-02 20:21:22

标签: vb.net visual-studio visual-studio-2012

我和我的朋友正在制作一个简单的课程,允许您根据字符串是设置为Processing还是Sales来确定工资单。我们希望它确定当您在文本框中放置Sales时,它会根据您输入的小时数计算出来。销售额乘以20。

这就是我们现在所拥有的。

Private Sub calcButton_Click(sender As Object, e As EventArgs) Handles calcButton.Click

        'declare

        Dim total As Double

        Dim weekly As Double
        Dim weekly1 As Double
        Dim weekly2 As Double
        Dim weekly3 As Double
        Dim weekly4 As Double


        Dim hours1 As Double
        Dim hours2 As Double
        Dim hours3 As Double
        Dim hours4 As Double

        Dim dep1 As Integer
        Dim dep2 As Integer
        Dim dep3 As Integer
        Dim dep4 As Integer

        Double.TryParse(hoursWorked1TB.Text, hours1)
        Double.TryParse(hoursworked2TB.Text, hours2)
        Double.TryParse(hoursWorked3TB.Text, hours3)
        Double.TryParse(hoursWorked4TB.Text, hours4)


        'as decimal


        weeklyPay1.Text = weekly.ToString("C1")
        weeklyPay2.Text = weekly.ToString("C1")
        weeklyPay3.Text = weekly.ToString("C1")
        weeklyPay4.Text = weekly.ToString("C1")


        'sales and processing

        If dep1TB.Text = "Sales" Then
            dep1 = 20
        ElseIf dep1TB.Text = "Processing" Then
            dep2 = 15
        Else : dep1 = 0
        End If

        If dep2TB.Text = "Sales" Then
            dep2 = 20
        ElseIf dep2TB.Text = "Processing" Then
            dep2 = 15
        Else : dep2 = 0
        End If

        If dep3TB.Text = "Sales" Then
            dep3 = 20
        ElseIf dep3TB.Text = "Processing" Then
            dep3 = 15
        Else : dep3 = 0
        End If

        If dep4TB.Text = "Sales" Then
            dep4 = 20
        ElseIf dep4TB.Text = "Processing" Then
            dep2 = 15
        Else : dep4 = 0
        End If

        'calculations

        weekly1 = hours1 * dep1
        weekly2 = hours2 * dep2
        weekly3 = hours3 * dep3
        weekly4 = hours4 * dep4

        total = weekly1 + weekly2 + weekly3 + weekly4

    End Sub

后来我确定我们需要一个As String变量,但所有东西仍然返回0。

1 个答案:

答案 0 :(得分:0)

我假设您的代码以下注释行是拼写错误:

    If dep1TB.Text = "Sales" Then
        dep1 = 20
    ElseIf dep1TB.Text = "Processing" Then
        dep2 = 15 ' should `dep1` instead?
    Else : dep1 = 0
    End If

在这里:

    If dep4TB.Text = "Sales" Then
        dep4 = 20
    ElseIf dep4TB.Text = "Processing" Then
        dep2 = 15 ' should `dep4` instead?
    Else : dep4 = 0
    End If

如果是这样,您可以像这样重写代码:

Private Sub calcButton_Click(sender As Object, e As EventArgs)

    Dim total As Double

    Dim hoursWorkedTBs() As TextBox = { hoursWorked1TB, hoursWorked2TB, hoursWorked3TB, hoursWorked4TB }
    Dim weeklyPayTBs() As TextBox = { weeklyPay1, weeklyPay2, weeklyPay3, weeklyPay4 }
    Dim depTBs() As TextBox = { dep1TB, dep2TB, dep3TB, dep4TB }
    Dim weekly(4) As Double
    Dim hours(4) As Double
    Dim dep(4) As Integer

    For i = 0 to 3
        hours(i) = double.Parse(hoursWorkedTBs(i).Text)
        If depTBs(i).Text = "Sales" Then
            dep(i) = 20
        ElseIf depTBs(i).Text = "Processing" Then
            dep(i) = 15
        Else
            dep(i) = 0
        End If
        weekly(i) = hours(i) * dep(i)
        weeklyPayTBs(i).Text = weekly(i).ToString("C1")
    Next

    total = weekly.Sum()

End Sub

请注意,在执行计算后,每周付费文本框会更新。在大多数情况下,这就是您的代码出错的地方。