Vb.net 如何让案例发挥作用
我试图回答一个问题,在这个问题中,我必须使用VisualBasic中的用例 问题是: 编写一个程序,询问用户本周的工作小时数和他们的小时工资率。该程序用于计算草地工资。如果工作时间超过40小时,则额外的工作时间将按费率的1.5倍支付。如果工作小时数不在0到60之间,程序应显示错误消息 这是我写的代码,任何关于出错的想法都将不胜感激Vb.net 如何让案例发挥作用,vb.net,visual-studio,Vb.net,Visual Studio,我试图回答一个问题,在这个问题中,我必须使用VisualBasic中的用例 问题是: 编写一个程序,询问用户本周的工作小时数和他们的小时工资率。该程序用于计算草地工资。如果工作时间超过40小时,则额外的工作时间将按费率的1.5倍支付。如果工作小时数不在0到60之间,程序应显示错误消息 这是我写的代码,任何关于出错的想法都将不胜感激 Console.WriteLine("This will calculate the gross profit from the hours you work and
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()
Console.WriteLine(“这将计算您工作时间和小时工资的毛利”)
Console.WriteLine(“您的工作时间不能低于0或高于60,
如果你工作超过40小时,额外的工作时间将是原来的1.5倍
Console.Write(“请输入您每周的工作时间:”)
整数形式的Dim小时数=Console.ReadLine()
控制台。写入(“请输入您获得的小时工资:”)
Dim hourlyPay As Decimal=Console.ReadLine()
Dim message As String=“您每周的总工资为:£”
暗淡的星期日为十进制
选择病例时数
病例040和时数<60
周薪=((40*hourlyPay)+((小时-40)*(hourlyPay*1.5)))
病例时数<0或时数>60
message=“很抱歉,您输入的小时数超出范围,请重试”
结束选择
Console.WriteLine(消息和weeklyPay.ToString(“N2”))
Console.ReadLine()
选择
以先到先得的方式工作,通过测试条件并按顺序处理,您可以消除每个条件,以便剩下的都是“默认”条件
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
选择案例时间
病例>60例
message=“很抱歉,您输入的小时数超出范围,请重试”
病例<0
message=“很抱歉,您输入的小时数低于范围,请重试”
病例>40例
周薪=((40*hourlyPay)+((小时-40)*(hourlyPay*1.5)))
其他情况
周薪=小时*小时薪
结束选择
我想出了解决办法:
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
Console.WriteLine(“这将计算您工作时数和小时工资的毛利”)”发送此程序所做工作的消息
Console.WriteLine(“您的工作时间不能低于0或高于60,
如果你的工作时间超过40小时,那么额外的工作时间将是原来的1.5倍。”“解释了界限和条件
Console.ReadLine()
Console.Write(“请输入您每周的工作时间:”)要求输入工作时间
Dim hours As Integer=Console.ReadLine()将小时声明为整数,并为用户输入的内容设置其值
Console.Write(“请输入小时工资:”)询问小时工资
Dim hourlyPay As Decimal=Console.ReadLine()将hourlyPay声明为十进制,并为用户输入的内容设置其值
Dim message As String=“您每周的总工资为:”,将消息声明为字符串,并设置其值以告诉用户他们的工资是多少-这在以后会很有用
Dim weeklyPay As Decimal'将周工资声明为十进制
Select Case hours的if语句使用起来很复杂,所以我使用了Select Case语句
案例0至40'当工作时间在0至40小时之间时,则周工资正常(小时工资*小时)
周薪=小时*小时薪
案例40至60'但是,如果工时超过40小时,则额外工时(工时-40小时)将以更高的费率支付(1.5倍)
周薪=((40*hourlyPay)+((小时-40)*(hourlyPay*1.5)))
案例<0'当小时数低于0时,则发送错误消息
message=“很抱歉,您输入的小时数在范围内,请重试”
案例>60'当小时数超过60时,则发送错误消息
message=“很抱歉,您输入的小时数超出范围,请重试”
结束选择
Console.WriteLine(message&weeklyPay.ToString(“N2”))将消息发送给用户及其付费用户
Console.ReadLine()'停止程序终止
我们怎么知道出了什么问题?另外,请阅读并采取行动。同时打开选项Strict,如果我为您的任何输入键入“ABC”,该怎么办?请记住,整数/小数不是字符串,您不应该依赖编译器来简化操作。请注意,例如,完全不处理40或60个小时。学习条件运算符=。