如何让案件工作

时间:2017-06-17 12:17:53

标签: vb.net visual-studio

我想回答一个问题,我必须在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 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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
相关问题