【问题标题】:Liberty Basic Payroll Calculator IF ELSELiberty 基本工资计算器 IF ELSE
【发布时间】:2015-05-31 01:54:28
【问题描述】:

我刚刚开始使用 Sams 学习基本编程,并且已经遇到了我一直在尝试编写的简单工资计算器的问题。有人可以解释我如何更好地重写这个吗?并且您只能在 IF THEN 语句之后使用打印命令吗?我想我必须投资一本更新的书,但我希望至少在此期间我可以让它运行。

Input "Please input payrate: "; Ans$
    Print
Input "Please input hours worked: "; Hrs$
If (Hrs$ >= "40") Then
    payRate = (Hrs$ * Ans$)
Else If (Hrs$ <= "41") Then
    payRate = Hrs$ * (1.5 * Ans$)
End If

taxRate = payRate * .15
grossPay = payRate * Hrs$
netPay = payRate - taxRate

Print

Print: "Your net pay is: "; netPay

【问题讨论】:

    标签: calculator basic


    【解决方案1】:

    我不使用 Liberty,但我想当您尝试对字符串进行数学运算时它会抱怨。此外,我发现您对“payrate”名称的滥用令人困惑。加班计算的逻辑也是错误的。这是我第一次尝试重写:

    Input "Please input payrate: "; Rate$
    Rate = val(Rate$)
    Print
    Input "Please input hours worked: "; Hrs$
    Hrs = val(Hrs$)
    Gross = Hrs * Rate
    If Hrs > 40 Then Gross = Gross + ((Hrs - 40) * Rate / 2))
    Taxes = Gross * .15
    Net = Gross - Taxes
    Print
    Print: "Your gross pay is: "; Gross
    Print: "Your net pay (after $";Taxes;" in taxes) is: "; Net
    

    【讨论】:

      【解决方案2】:

      此代码将在 Liberty BASIC 中运行:

      INPUT "Please enter hours worked: "; hours 'you don't need a dollar sign at the end of the variable because it's a value not a string
      INPUT "Please enter your hourly rate: "; hourlyRate
      gross = hours * hourlyRate 'works out the gross amount
      IF (hours > 40) THEN 'if the person worked more than 40 hours, then the following command is applicable...
      gross = 40 * hourlyRate + ((hours - 40) * (hourlyRate * 1.5)) 'this line calculates the money earned for 40 hours, and adds the remaining hours at 1.5 times the hourly rate
      END IF 'end the IF condition
      tax = gross * 0.15 'calculates the tax at 15%
      net = gross - tax 'calculates the net pay (i.e., gross pay after tax)
      PRINT "-------------------------------"
      PRINT "Your gross pay is: "; gross
      PRINT "Your net pay is: "; net
      

      正如上一张海报所建议的那样,您应该在数学中使用值而不是字符串(值的末尾没有美元符号),并且当您标记变量时,它们应该具有与您的内容相关的有意义的名称正在努力实现。

      Liberty BASIC 在帮助下有一个教程,您可以使用它来自学如何编码。否则,Beginning Programming for Dummies 是另一个有用的指南 - 这就是我正在使用的 :)

      【讨论】:

        猜你喜欢
        • 2013-08-10
        • 1970-01-01
        • 2013-11-25
        • 2013-09-10
        • 2011-07-17
        • 1970-01-01
        • 1970-01-01
        • 2018-11-04
        • 1970-01-01
        相关资源
        最近更新 更多