-2

我为我的一门课程编写了这段代码(请记住这是初学者课程,我们刚刚开始学习 else if)。它应该写下某人的姓名、SSN、工资、时间和最后的税款。

<!DOCTYPE html>
<html lang="en"
<!-- Author: Sidney Stephens
Assignment ? - Part ?: Title: ?
Date: [[date:%d/%m%Y %H:%M"%S]]
Description: ???
-->

<head>
    <meta charset="UTF-8" <!-- Character Setter -->
    <title>CMIS102 &raquo; Sidney Stephens &raquo; Assignment ? Part ? </title>
    <script>
    document.write("<h1>Payroll Calculator</h1>")
    window.alert("This is the Dynasty Casino Payroll Program.");
    sName = window.prompt("Who's payroll?","");
    document.write("<p>Name: </p>" + sName);
    sSSN = window.prompt("What is their SSN?","");
    document.write("<p>SSN: </p>" + sSSN);;
    sEntry = window.prompt("Hourly Rate?","0");
    var fRate = parseFloat(sEntry);
    document.write("<p>Rate: </p>" + fRate);
    sEntry = window.prompt("How many hours worked?","0");
    var fTime = parseFloat(sEntry);
    document.write("<p>Time: </p>" + fTime);
    if (fTime > 40)
    {
        var sDiff = fTime - 40;
        var fDiff = parseFloat(sDiff);
        document.write("<p>Overtime Worked: </p>" + fDiff);
        var sRaw = (fRate * 40) + ((fRate * 1.5) * fDiff);
        var fRaw = parseFloat(sRaw);
        document.write("<p>Pay Without Taxes: </p>" + fRaw);
        if (fRaw <= 1000)
        {
            var sFinal = fRaw * 0.85;
            var fFinal = parseFloat(sFinal);
            window.alert("You will recieve $" + fFinal);
            document.write("<p>Final Pay: </p>" + fFinal);
        }

        else if (fRaw > 1000)
        {
            var sExtra = fRaw - 1000;
            var fExtra = parseFloat(sExtra);
            var sfinal = (fExtra * 0.75) + (1000 * 0.85);
            window.alert(sFinal);
            var fFinal = parseFloat(sFinal);
            window.alert("You will recieve $" + sFinal);
            document.write("<p>Final Pay: </p>" + sFinal);
        }
    }

    else if (fTime <= 40)
    {
        var sRaw = 40 * fRate;
        var fRaw = parseFloat(sRaw);
        document.write("<p>Pay Without Taxes: </p>" + fRaw);
        if (fRaw <= 1000)
        {
            var sFinal = fRaw * 0.85;
            var fFinal = parseFloat(sFinal);
            window.alert("You will recieve $" + fFinal);
            document.write("<p>Final Pay: </p>" + fFinal);
        }

        else if (fRaw > 1000)
        {
            var sExtra = fRaw - 1000;
            var fExtra = parseFloat(sExtra);
            var sfinal = ((fExtra * 0.75)) + ((1000 * 0.85));
            window.alert(sFinal);
            var fFinal = parseFloat(sFinal);
            window.alert("You will recieve $" + fFinal);
            document.write("<p>Final Pay: </p>" + fFinal);
        }
    }
    </script>
</head>

<body style="color: #000000; background-color: #FFFFFF">
    <p>This is the template</p>
</body>

它对我来说很好,除非 sRaw 超过 1000,然后由于某种原因它一直说 NaN 或未定义。我们还没有真正深入了解它的吸引力。我们几乎已经让算法发挥作用了。先感谢您。

4

1 回答 1

0

您的代码中有错字

else if (fRaw > 1000)
        {
            var sExtra = fRaw - 1000;
            var fExtra = parseFloat(sExtra);
            var sfinal = (fExtra * 0.75) + (1000 * 0.85);//here, change sfinal  to sFinal
            window.alert(sFinal);
            var fFinal = parseFloat(sFinal);
            window.alert("You will recieve $" + sFinal);
            document.write("<p>Final Pay: </p>" + sFinal);
        }

花了很多时间才找到这个简单的小错字!!你应该学习的道德“错字;永远不要让它们发生”

于 2013-09-14T18:50:50.107 回答