2015-03-02 111 views
-3

我和我的朋友正在研究一個簡單的課程程序,允許您根據字符串設置爲ProcessingSales來確定工資。我們希望它確定,當您將Sales放在文本框中時,它會計算出您輸入的小時數。銷售額乘以20.Visual Basic If..Then..Else String statement

這是我們目前所擁有的。

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 

後來我決定了我們需要一個作爲字符串變量,但一切仍然返回爲0

+4

您需要在完成計算後設置文本框的文本*。 – 2015-03-02 20:25:27

+1

似乎你有Option Strict On,我不得不在這一倡議中向你表示祝賀。 – 2015-03-02 20:37:55

+0

我看到了Tripp的編輯,並在weeklyPay需要相應的數字以便在正確的文本框中打印後才意識到它。謝謝你的幫助。 – user3011165 2015-03-02 20:38:58

回答

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 

注意進行計算的週薪文本框將被更新。大部分情況下,這就是代碼出錯的地方。