javascript做,而不是写最后一个循环

时间:2013-02-15 14:08:11

标签: javascript do-while

我正在做一些功课,最后一个问题是踢我的后方,每次我跑,它都不会显示最后输入的信息循环。因此输入3个循环,只会显示2个。

感谢您的帮助。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Casino_Eric</title>
    <script type="text/javascript">

        document.write( "<h1>Casino_Eric</h1>" );
        var CashierID, CustomerCount=0, TotalChips, OverAllValue, EndOrContinue;
        var BlueValue = 5, BlackValue = 20, RedValue = 50, GreenValue = 100;
        var BlueQty, BlackQty, RedQty, GreenQty;
        var BlueResult, BlackResult, RedResult, GreenResult;

        CashierID = window.prompt("Enter 6 digit cashier ID.", "000000");
            do{
                BlueQty = window.prompt("Enter Number of Blue Chips", "0");
                BlackQty = window.prompt("Enter Number of Black Chips", "0");
                RedQty = window.prompt("Enter Number of Red Chips", "0");
                GreenQty = window.prompt("Enter Number of Green Chips", "0");

                BlueResult = BlueQty * BlueValue;
                BlackResult = BlackQty * BlackValue;
                RedResult = RedQty * RedValue;
                GreenResult = GreenQty * GreenValue;
                OverAllValue = BlueResult + BlackResult + RedResult + GreenResult;


                EndOrContinue = parseInt(window.prompt("Would you like to end your shift now "+CashierID+" or count the chips for another customer?  Enter 1 to contiue, or N to quit.", "n"));
                    if(isNaN(EndOrContinue));
                    else{
                    CustomerCount++;
                    document.write("<p>Cashier ID: "+CashierID+", Customer : "+CustomerCount+"</br> Number of Blue Chips: "+BlueQty+", total value of Blue Chips is: "+BlueValue+"</br>Number of Black Chips: "+BlackQty+", total value of Black Chips is: "+BlackValue+"</br>Number of Red Chips: "+RedQty+", total value of Red Chips is: "+RedValue+"</br>Number of Green Chips: "+GreenQty+", total value of Green Chips is: "+GreenValue+"</br>This customer's total value is "+OverAllValue+".</p>");

                    }
                }
            while(!isNaN(EndOrContinue));


        </script>
    </head>
    <body>  
        <p>Reload for another conversion</p>
    </body>
</html>

2 个答案:

答案 0 :(得分:1)

这是由于if语句错位。现在它可以正常使用

 <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Casino_Eric</title>
        <script type="text/javascript">

            document.write( "<h1>Casino_Eric</h1>" );
            var CashierID, CustomerCount=0, TotalChips, OverAllValue, EndOrContinue;
            var BlueValue = 5, BlackValue = 20, RedValue = 50, GreenValue = 100;
            var BlueQty, BlackQty, RedQty, GreenQty;
            var BlueResult, BlackResult, RedResult, GreenResult;

            CashierID = window.prompt("Enter 6 digit cashier ID.", "000000");
                do{
                    BlueQty = window.prompt("Enter Number of Blue Chips", "0");
                    BlackQty = window.prompt("Enter Number of Black Chips", "0");
                    RedQty = window.prompt("Enter Number of Red Chips", "0");
                    GreenQty = window.prompt("Enter Number of Green Chips", "0");

                    BlueResult = BlueQty * BlueValue;
                    BlackResult = BlackQty * BlackValue;
                    RedResult = RedQty * RedValue;
                    GreenResult = GreenQty * GreenValue;
                    OverAllValue = BlueResult + BlackResult + RedResult + GreenResult;

                        document.write("<p>Cashier ID: "+CashierID+", Customer : "+CustomerCount+"</br> Number of Blue Chips: "+BlueQty+", total value of Blue Chips is: "+BlueValue+"</br>Number of Black Chips: "+BlackQty+", total value of Black Chips is: "+BlackValue+"</br>Number of Red Chips: "+RedQty+", total value of Red Chips is: "+RedValue+"</br>Number of Green Chips: "+GreenQty+", total value of Green Chips is: "+GreenValue+"</br>This customer's total value is "+OverAllValue+".</p>");


                    EndOrContinue = parseInt(window.prompt("Would you like to end your shift now "+CashierID+" or count the chips for another customer?  Enter 1 to contiue, or N to quit.", "n"));
                        if(!isNaN(EndOrContinue)){
                        CustomerCount++;
                        }
                    }while(!isNaN(EndOrContinue));


            </script>
        </head>
        <body>  
            <p>Reload for another conversion</p>
        </body>
    </html>

答案 1 :(得分:0)

作为一个“家庭作业”的例子/问题(感谢你的诚实)我的答案就是那种想法 - 你必须做自己的工作,但这应该有所帮助:

 EndOrContinue = parseInt(window.prompt("Would you like to end your shift now "+CashierID+" or count the chips for another customer?  Enter 1 to contiue, or N to quit.", "n"));
                    if(isNaN(EndOrContinue));
                    else{
                    CustomerCount++;
                    document.write("<p>Cashier ID: "+CashierID+", Customer : "+CustomerCount+"</br> Number of Blue Chips: "+BlueQty+", total value of Blue Chips is: "+BlueValue+"</br>Number of Black Chips: "+BlackQty+", total value of Black Chips is: "+BlackValue+"</br>Number of Red Chips: "+RedQty+", total value of Red Chips is: "+RedValue+"</br>Number of Green Chips: "+GreenQty+", total value of Green Chips is: "+GreenValue+"</br>This customer's total value is "+OverAllValue+".</p>");

                    }
                }
            while(!isNaN(EndOrContinue));

在这里你根据一个问题设置值 - 好吧。

  • 然后,您检查之前的问题设置对先前的条目执行任何操作
  • 然后您忘记(跳过)执行您之前的条目
  • 所需的操作
  • 然后再检查问题回复(再次)

为什么你第一次检查(在循环内)?你能删除吗?