2017-06-17 2 views
-2

私はビジュアルベーシックでケースを使用する必要がある質問に答えようとしています。ケースを働かせる方法

質問です:

はこの週労働時間数のユーザーと賃金の彼らの時給を要求するプログラムを書きます。このプログラムは、草の根の賃金を計算することです。労働時間数が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)また[ask]を読んで[tour]を取ってください。また、Option Strictを有効にしてください – Plutonix

+1

あなたの入力に「ABC」と入力するとどうなりますか?整数/小数点は文字列ではないことを覚えておいてください。そして、コンパイラでリレーしないでください。 Option Strict On – 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 
関連する問題