can';在这个javascript代码中找不到错误

can';在这个javascript代码中找不到错误,javascript,Javascript,它甚至没有跑。do while循环有问题。我想是因为所有的if语句?如果有人能帮我,我将不胜感激。谢谢 do { entry = prompt("Enter taxable income as a valid number\n" + "Or enter 99999 to end entries", 99999); entry = parseInt(entry); // calculat

它甚至没有跑。do while循环有问题。我想是因为所有的if语句?如果有人能帮我,我将不胜感激。谢谢

        do {
        entry = prompt("Enter taxable income as a valid number\n" +
                       "Or enter 99999 to end entries", 99999);
        entry = parseInt(entry);

        // calculate the tax owed here
        if (entry < 0){
            document.write("Please enter a positive number");
        }
        if (entry > 0 && entry < 8701){
            tax_owed = entry * 0.10;
        }
        if (entry > 8700 && entry < 35351){
            tax_owed = 870 * 0.15;
            tax_owed += entry;
        }
        if (entry > 35350 && entry < 85651){
            tax_owed = 4867 * 0.25;
            tax_owed += entry;
        }
        if (entry > 85650 && < 178651){
            tax_owed = 17442 * 0.28;
            tax_owed += entry;
        }
        if (entry > 178650 && entry < 388351){
            tax_owed = 43482 * 0.33;
            tax_owed += entry;
        }
        if (entry > 388350){
            tax_owed = 112683 * 0.35;
            tax_owed += entry;
        }

        alert("Tax owed is " + tax_owed);
    }
    while (entry != 99999);
do{
输入=提示(“输入应纳税所得额作为有效数字\n”+
“或输入99999结束输入”,99999);
entry=parseInt(entry);
//计算这里所欠的税
如果(条目<0){
文件。填写(“请输入正数”);
}
如果(条目>0&&条目<8701){
欠税=分录*0.10;
}
如果(条目>8700和条目<351){
所欠税款=870*0.15;
所欠税款+=分录;
}
如果(条目>35350和条目<85651){
所欠税款=4867*0.25;
所欠税款+=分录;
}
如果(条目>85650&<178651){
所欠税款=17442*0.28;
所欠税款+=分录;
}
如果(条目>178650和条目<388351){
所欠税款=43482*0.33;
所欠税款+=分录;
}
如果(条目>388350){
所欠税款=112683*0.35;
所欠税款+=分录;
}
警报(“欠税为”+欠税);
}
while(条目=99999);
在您的21行中

if (entry > 85650 && < 178651){ // there is no operand between && and <.
if(entry>85650&&<178651){//在&&和85650之间没有操作数&&entry<178651){

if(条目>85650&<178651)
如果仔细查看,您会发现您没有将第二个数字与任何东西进行比较。它应该是
if(条目>85650&&entry<178651)

已经找到了
<178651
之前缺少操作数的错误,但是如果您希望代码中的潜在错误更少,我建议使用一个简单的重构过程

  • 使用
    else if
    将更有效。一旦找到与给定
    条目
    值对应的块,程序将停止检查其他条件。此外,您必须仅指定那些划分范围的值(“里程碑”),而不是重复它们,比如
    entry<8701
    ,然后是
    entry>8700
    。最后,它将修复另一个错误,当您输入99999时,它将打印
    所欠税款=…大约140000..

  • 更改循环的条件(引入布尔变量)将更具可读性

  • 我根本不是一个税务员,但你确定要添加
    欠税+=条目;
    ?我的意思是,我在申报10000美元时真的欠10130.5美元吗?在任何情况下,用1行而不是2行会更易读:
    欠税=条目+17442*0.28;//但你真的添加了“条目”?

因此,代码如下所示:

continueEntering = true;

while ( continueEntering ) {
    tax_owed = 0;
    entry = prompt("Enter taxable income as a valid number\n" + "Or enter 99999 to end entries", 99999);
        entry = parseInt(entry);

    if (entry == 99999) {
        continueEntering = false;
    } else if (entry < 0){
        alert("Please enter a positive number"); 
    } else if (entry <= 8700){
        tax_owed = entry * 0.10;
    } else if (entry <= 35350){
        tax_owed = 870 * 0.15 + entry;
    } else if (entry <= 85650){
        tax_owed = 4867 * 0.25 + entry;
    } else if (entry <= 178650){
        tax_owed = 17442 * 0.28 + entry;
    } else if (entry <= 388350){
        tax_owed = 43482 * 0.33 + entry;
    } else {
        tax_owed = 112683 * 0.35 + entry;
    }

    alert("Tax owed is " + tax_owed);
}

好的,我找到了计算税收的正确方法。你们说得对。我错了。我使用2012年所得税政策,因为这是我的教授希望我们使用的。再次感谢大家的帮助。我真的很喜欢这个网站!对我们n00bs来说太棒了

    var tax_owed = 0;
    var entry;

    continueEntering = true;

    while (continueEntering) {
        tax_owed = 0;
        entry = prompt("Enter taxable income as a valid number\n" + "Or enter 99999 to end entries", 99999);
        entry = parseInt(entry);

        if (entry == 99999) {
            continueEntering = false;
        } else if (entry < 0){
            alert("Please enter a positive number"); 
        } else if (entry <= 8700){
            tax_owed = entry * 0.10;
        } else if (entry <= 35350){
            tax_owed = entry - 8700;
            tax_owed *= 0.15;
            tax_owed += 870;
        } else if (entry <= 85650){
            tax_owed = entry - 35350;
            tax_owed *= 0.25;
            tax_owed += 4867;
        } else if (entry <= 178650){
            tax_owed = entry - 85650;
            tax_owed *= 0.28;
            tax_owed += 17442;
        } else if (entry <= 388350){
            tax_owed = entry - 178650;
            tax_owed *= 0.33;
            tax_owed += 43482;
        } else if (entry > 388350){
            tax_owed = entry - 388350;
            tax_owed *= 0.35;
            tax_owed += 112683;
        }

        alert("Tax owed is " + tax_owed.toFixed(2));
var-tax\u欠款=0;
风险价值分录;
continueEntering=true;
同时(继续){
所欠税款=0;
分录=提示(“输入应纳税所得额作为有效数字\n”+”或输入99999结束分录”,99999);
entry=parseInt(entry);
如果(条目==99999){
连续性=假;
}else if(条目<0){
警报(“请输入正数”);

}else if(输入这些神奇的数字是什么?为什么不将它们分配给变量?通过重构代码,您可能会发现错误。如您所见,在
else if中(最后输入,您可以添加
所欠税款+=0
,这不会改变其值,但从现在起,您的6个块的格式都完全相同!这意味着我们可以通过6个元素创建3个数组,并使用
for
而不是6个
if
块。只需检查一下有多简单和安全(没有bug)从现在起,它将改变一些值或添加/删除一些“税收阈值”!希望您已经了解了。请参阅我的主要答案(已接受的答案)这看起来好多了。我对数组和for循环有基本的了解,但在这种情况下甚至没有考虑使用它们。我在理解for循环的这一部分时有点困难,尽管I=tax_threshold.length-1;I>=0;I--我知道如何阅读它,只是不知道它是如何应用的。为什么我--还有不是i++?那么-1在tax_threshold.length末尾做了什么?@user3733575,好问题,-1只是因为当数组
a
有6个元素时,
a.length
等于6,而要访问数组元素,需要从0开始:第一个元素是a[0],然后是a[1],a[2],a[3],a[4],a[5]。这就是为什么我们对最后一个元素使用
length-1
。现在,
用于(i=tax\u threshold.length-1;i>=0;i--)
意味着我们只是简单地颠倒循环的顺序:我们从i=5开始,向下到i=0。为什么?在这个特殊的税务问题上更方便。你看,你试图将
条目
定位到其中一个范围。范围N的上限是范围N+1的下限。但是最后一个范围呢?它不是h有一个上限!所以,我们可以将所有6个范围表示为:[超过0],[超过8700],…,[超过388350]。好的,我明白了你所说的一切,除了这一部分:“范围N的上限是范围N+1的下限。但是最后一个范围呢?它没有上限!所以,我们可以将所有6个范围表示为:[超过0],[超过8700],…,[超过388350]。“谢谢你的帮助哦,等等,我想我现在明白了。因为条目需要介于两个数字之间,所以你必须从最高的开始,然后一路向下。对吗?
continueEntering = true;

while ( continueEntering ) {
    tax_owed = 0;
    exitValue = 99999;

    tax_threshold = [ 0, 8700, 35350, 85650, 178650, 388350 ];
    tax_rate = [ 0.10, 0.15, 0.25, 0.28, 0.33, 0.35 ];
    additional_tax = [ 0, 870, 4867, 17442, 43482, 112683 ];

    entry = prompt("Enter taxable income as a valid number\n" + 
        "Or enter " + exitValue + " to end entries", exitValue);
    entry = parseInt(entry);

    if (entry == exitValue) {
        continueEntering = false;
    } else if (entry < 0){
        alert("Please enter a positive number"); 
    } else {

        for( i = tax_threshold.length-1; i >= 0; i-- ) {
            if ( entry > tax_threshold[i] ) {
                tax_owed = entry - tax_threshold[i];
                tax_owed *= tax_rate[i];
                tax_owed += additional_tax[i];
                break;
            }
        }
    }

    alert("Tax owed is " + tax_owed.toFixed(2));
}
    var tax_owed = 0;
    var entry;

    continueEntering = true;

    while (continueEntering) {
        tax_owed = 0;
        entry = prompt("Enter taxable income as a valid number\n" + "Or enter 99999 to end entries", 99999);
        entry = parseInt(entry);

        if (entry == 99999) {
            continueEntering = false;
        } else if (entry < 0){
            alert("Please enter a positive number"); 
        } else if (entry <= 8700){
            tax_owed = entry * 0.10;
        } else if (entry <= 35350){
            tax_owed = entry - 8700;
            tax_owed *= 0.15;
            tax_owed += 870;
        } else if (entry <= 85650){
            tax_owed = entry - 35350;
            tax_owed *= 0.25;
            tax_owed += 4867;
        } else if (entry <= 178650){
            tax_owed = entry - 85650;
            tax_owed *= 0.28;
            tax_owed += 17442;
        } else if (entry <= 388350){
            tax_owed = entry - 178650;
            tax_owed *= 0.33;
            tax_owed += 43482;
        } else if (entry > 388350){
            tax_owed = entry - 388350;
            tax_owed *= 0.35;
            tax_owed += 112683;
        }

        alert("Tax owed is " + tax_owed.toFixed(2));