如何计算Visual Basic中的用户输入?

时间:2016-11-15 19:46:53

标签: vb.net visual-studio count counter

VB的代码:

For 'I think this where the counter would go if I knew how to write it
        If (intEmpCount < 10) Then
            If decTotalTax < decGrossPay Then
                decNetPay = decNetPay
                boxResults.Items.Add("EMPLOYEE ID: " & dblEmpID.ToString)
                boxResults.Items.Add("Gross Pay: $" & decGrossPay.ToString("N2"))
                boxResults.Items.Add("Net Pay: $" & decNetPay.ToString("N2"))
                boxResults.Items.Add("State Withholdings: $" & decStateTaxAmount.ToString("N2"))
                boxResults.Items.Add("Federal Withholdings: $" & decFederalTaxAmount.ToString("N2"))
                boxResults.Items.Add("FICA Withholdings: $" & decFICAAmount.ToString("N2"))
            Else
                MessageBox.Show("ERROR. THE AMOUNT OF WITHOLDINGS ($" & decTotalTax.ToString("N2") & ") EXCEEDS THE GROSS PAY.")
            End If
        Else
            MessageBox.Show("ERROR. YOU HAVE EXCEEDED THE ALLOWABLE NUMBER OF EMPLOYEES.")
        End if

要回答的问题:我如何计算已输入的EmployeeID数量?

对于这项任务,我假设教授希望我们将员工人数限制在10人,所以我想弄清楚如何做到这一点。同样,出于此分配的目的,我们应该假设EmployeeID可以是长度取决于公司规模的六位或七位数字。

例如:1000 10001 100001 101000等等......

1 个答案:

答案 0 :(得分:0)

这是Visual Basic中for循环的一般语法:

For counter [ As datatype ] = start To end [ Step step ]  
    [ statements ]  
    [ Continue For ]  
    [ statements ]  
    [ Exit For ]  
    [ statements ]  
Next [ counter ]

所以你的循环可能看起来像

For number As Integer = 0 To 5
            (something here)
        Next
        (something here)

所以这意味着我们最初声明一个名为number的整数= 0。它将一直持续到达到5.你会写类似的东西,但你必须弄清楚循环结束时的条件(例如你的可能是10)。我还没有用VB编写一段时间,但我主要做的是C#,它有点类似。您的代码可能如下所示:

    For k As Integer = 0 To 5
            If (intEmpCount < 10) Then
                If decTotalTax < decGrossPay Then
                    decNetPay = decNetPay
                    boxResults.Items.Add("EMPLOYEE ID: " & dblEmpID.ToString)
                    boxResults.Items.Add("Gross Pay: $" & decGrossPay.ToString("N2"))
                    boxResults.Items.Add("Net Pay: $" & decNetPay.ToString("N2"))
                    boxResults.Items.Add("State Withholdings: $" & decStateTaxAmount.ToString("N2"))
                    boxResults.Items.Add("Federal Withholdings: $" & decFederalTaxAmount.ToString("N2"))
                    boxResults.Items.Add("FICA Withholdings: $" & decFICAAmount.ToString("N2"))
                Else
                    MessageBox.Show("ERROR. THE AMOUNT OF WITHOLDINGS ($" & decTotalTax.ToString("N2") & ") EXCEEDS THE GROSS PAY.")
                End If
            Else
                MessageBox.Show("ERROR. YOU HAVE EXCEEDED THE ALLOWABLE NUMBER OF EMPLOYEES.")
            End if
Next

<强>替代地

如果你想要,你可以在for循环之前创建一个整数变量,每次迭代加上+1。所以你可能会尝试类似的东西:

Dim k As Integer = 0
    For 'I think this where the counter would go if I knew how to write it
            If (intEmpCount < 10) Then
                If decTotalTax < decGrossPay Then
                    decNetPay = decNetPay
                    boxResults.Items.Add("EMPLOYEE ID: " & dblEmpID.ToString)
                    boxResults.Items.Add("Gross Pay: $" & decGrossPay.ToString("N2"))
                    boxResults.Items.Add("Net Pay: $" & decNetPay.ToString("N2"))
                    boxResults.Items.Add("State Withholdings: $" & decStateTaxAmount.ToString("N2"))
                    boxResults.Items.Add("Federal Withholdings: $" & decFederalTaxAmount.ToString("N2"))
                    boxResults.Items.Add("FICA Withholdings: $" & decFICAAmount.ToString("N2"))
                    k += 1
                Else
                    MessageBox.Show("ERROR. THE AMOUNT OF WITHOLDINGS ($" & decTotalTax.ToString("N2") & ") EXCEEDS THE GROSS PAY.")
                End If
            Else
                MessageBox.Show("ERROR. YOU HAVE EXCEEDED THE ALLOWABLE NUMBER OF EMPLOYEES.")
            End if

然后整数k显然是你做任何事情的价值。如果不是这样,它肯定会非常相似。