如何计算Visual Basic中的用户输入?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何计算Visual Basic中的用户输入?相关的知识,希望对你有一定的参考价值。
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等...
答案
这是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显然是你做任何事情的价值。如果不是这样,它肯定会非常相似。
以上是关于如何计算Visual Basic中的用户输入?的主要内容,如果未能解决你的问题,请参考以下文章
在 Visual Basic 2010 中的表单中的选项卡之间传递值
如何在Visual Basic 2012中使用mdb数据库中的数据填充列表框?