2017-06-17 72 views
-2

我想回答一個問題,我必須在visual basic中使用案例。如何讓案件工作

的問題是:

編寫一個程序,要求用戶進行的小時數在本週和他們的工資每小時工資標準。該計劃是計算草薪水。如果工作的小時數大於40,那麼額外的小時數將以1.5倍的費率支付。如果工作的小時數不在0到60的範圍內,程序應顯示一條錯誤消息。

這是我寫的代碼,關於出錯的任何想法將不勝感激。在第一

Console.WriteLine("This will calculate the gross profit from the hours you work and the hourly pay") 
    Console.WriteLine("Your work hours can not be under 0 or above 60, 
    if you work above 40 hours the extra hours will be 1.5 times the original") 

    Console.Write("Please enter the hours you work a week: ") 
    Dim hours As Integer = Console.ReadLine() 
    Console.Write("Please enter the hourly pay you get: ") 
    Dim hourlyPay As Decimal = Console.ReadLine() 

    Dim message As String = "Your gross pay per week is: £" 

    Dim weeklyPay As Decimal 

    Select Case hours 
     Case 0 < hours < 40 
      weeklyPay = hours * hourlyPay 
     Case hours > 40 And hours < 60 
      weeklypay = ((40 * hourlyPay) + ((hours - 40) * (hourlyPay * 1.5))) 
     Case hours < 0 Or hours > 60 
      message = "Sorry the hours you entered are above the range try again" 
    End Select 

    Console.WriteLine(message & weeklyPay.ToString("N2")) 
    Console.ReadLine()   
+2

我們怎麼知道哪裏出了問題? –

+1

[Select Case Docs](https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/select-case-statement)和[Select Case Statement](https:// msdn.microsoft.com/VBA/Language-Reference-VBA/articles/select-case-statement)也請閱讀[問]並參加[旅遊]。同時打開Option Strict – Plutonix

+1

如果我爲任何輸入內容輸入「ABC」,該怎麼辦?記住整數/小數不是字符串,你不應該在編譯器上中繼來讓你更容易。選項嚴格在 – Steve

回答

2

Select作品來先通過測試條件,以便您可以消除各種條件,使所有剩下的工作就是「默認」狀態下處理這些服務的基礎。

Select Case hours 
    Case Is > 60 
     message = "Sorry the hours you entered are above the range try again" 
    Case Is < 0 
     message = "Sorry the hours you entered are below the range try again" 
    Case Is > 40 
     weeklypay = ((40 * hourlyPay) + ((hours - 40) * (hourlyPay * 1.5))) 
    Case Else 
     weeklyPay = hours * hourlyPay 
End Select 

MSDN - Select...Case Statement (Visual Basic)

0

我想通了,如何解決這個問題:

Console.WriteLine("This will calculate the gross profit from the hours you work and the hourly pay")  'sends the message of what this program does 
    Console.WriteLine("Your work hours can not be under 0 or above 60, 
    if you work above 40 hours the extra hours will be 1.5 times the original")        'explains the boundaries and conditions 
    Console.ReadLine() 
    Console.Write("Please enter the hours you work a week: ")            'asks for the hours worked 
    Dim hours As Integer = Console.ReadLine()                'declares hours as an integer and sets it's value for what the user enters 
    Console.Write("Please enter the hourly rate of pay: ")             'asks for the hourly rate of pay 
    Dim hourlyPay As Decimal = Console.ReadLine()               'declares hourlyPay as a decimal and sets it's value for what the user enters 
    Dim message As String = "Your gross pay per week is: £"             'declares message as a string and sets it's value to tell the user what their pay is - this will be useful later 
    Dim weeklyPay As Decimal                     'declares weekly pay as a decimal 
    Select Case hours                      'An if statement would complicated to use so, i used a select case 
     Case 0 To 40                       'when hours are between 0 and 40, then the weekly pay is normal (hourly rate * hours) 
      weeklyPay = hours * hourlyPay 
     Case 40 To 60                      'however when hours are above 40, then the extra hours (hours - 40) are paid at a higher rate (1.5 times) 
      weeklyPay = ((40 * hourlyPay) + ((hours - 40) * (hourlyPay * 1.5))) 
     Case < 0                        'when hours are under the boundary of 0 then send the error message 
      message = "Sorry the hours you entered are under the range try again " 
     Case > 60                       'when hours are over the boundary of 60 then send the error message 
      message = "Sorry the hours you entered are above the range try again " 
    End Select 
    Console.WriteLine(message & weeklyPay.ToString("N2"))             'this sends the message across to the user and their pay 
    Console.ReadLine()                      'stops the program from terminating