2011-11-03 195 views
0

我需要在do/while語句循環中循環javascript的幫助。Javascript do-while循環問題

我遇到的問題是,我不想在輸入無效產品時在表格中顯示不正確的信息,但它確實顯示在document.write中。我需要幫助才能確保不會顯示不正確的信息。

此外,當我點擊「確定」添加更多我的訂單,它不會循環它,而只是顯示document.write。如果您點擊「確定」按鈕,我想讓它循環。

謝謝你的幫助。

下面是代碼:

<html> 

    <head><title>Javascript Assignment 3: Daniel Weiner</title> 
     </head> 
    <body bgcolor="brown"> 

    <h1 align="center">Big Ben's Burgers-eCommerce</h1> 

    <table border="1" width="100%" height="450px" bgcolor="gray"><tr> 
     <th align="center">Product/Service</th> 
     <th align="center">Price</th> 
     <th align="center">Discount</th> 
    </tr> 

    <tr bgcolor="orange"> 
     <td align="center"><font size="3">Hamburger 
    <br> 
    <br> 
    <a href="http://www.mcdonalds.com/us/en/food/product_nutrition.sandwiches.256.Hamburger.html" target="_blank">Classic Hamburger</a> 

     <td align="right" bgcolor="orange"><font size="3">$8.00</font></td> 
     <td align="center" bgcolor="orange"><font size="3">.10</font></td> 
    </tr> 

    <tr bgcolor="orange"> 
     <td align="center"><font size="3">Cheeseburger 
    <br> 
    <br> 
    <a href="http://www.mcdonalds.com/us/en/food/product_nutrition.sandwiches.284.cheeseburger.html" target="_blank">Classic Cheeseburger </a> 
    </font></td> 
     <td align="right" bgcolor="orange"><font size="3">$9.00</font></td> 
     <td align="center" bgcolor="orange"><font size="3">.05</font></td> 
    </tr> 

    <tr bgcolor="orange"> 
     <td align="center"><font size="3">Soda 
    <br> 
    <br> 
    <a href="http://www.mcdonalds.com/us/en/food/full_menu/beverages.html" target="_blank">Fabulous Drinks</a> 
    </font></td> 
     <td align="right" bgcolor="orange"><font size="3">$2.00 
    </font></td> 
    <td align="center" bgcolor="orange"><font size="3"> .07</font></td> 
    </tr> 

    <tr bgcolor="red"> 
     <td align="center"><font size="3"> French Fries 
    <br> 
    <br> 
    <a href="http://www.mcdonalds.com/us/en/food/product_nutrition.snackssides.120.small-french-fries.html" target="_blank"> Fries</a> 
    </font></td> 
     <td align="right" bgcolor="red"><font size="3"> $4.00</font></td> 
     <td align="center" bgcolor="red"><font size="3">.15</font></td> 

    </table> 
    <script type="text/javascript"> 
    /*Daniel Weiner, Fengpeng Yuan, Javascript 2, Nov 4,2011*/ 

    var username; 
    var bprice= 8; 
    var chprice= 9; 
    var sprice= 2; 
    var fprice= 4; 
    var price= 0; 
    var a; 
    var b; 
    var product= "hamburger, cheeseburger, soda, fries"; 
    var quantity =0; 
    var total; 
    var cost = 0; 
    var discount= 0; 
    do{ 
    username =prompt("Welcome to Big Ben's Burgers. Please enter your name.", ""); 
    alert("Hello " + username+". Please look through our available products and services before placing your order.",""); 
    product=prompt("What do you want?",""); 

    quantity =1*prompt("How many of " +product+ " would you like?"); 




    if (product == "hamburger") 
    { 
    price = bprice; 

    discount = .1; 
    } 
    else if (product == "cheeseburger") 
    { 
    price = chprice; 

    discount = .05; 
    } 

    else if (product == "soda") 
    { 
    price = sprice; 

    discount = .07; 
    } 

    else if (product == "fries") 
    { 
    price = fprice; 

    discount = .15; 
    } 

    else{ 
     alert("Sorry, " +username+ " Your item not found."); 
    } 
     cost=price*quantity 
     discount=price*discount*quantity 
     total=cost-discount 
    document.write("The cost of buying " +quantity+ " of " +product+ " is $" +cost+ ".<br/>"); 
    document.write("This discount for this purchase is $" +discount+ ".<br/>"); 

    }while(a==false) 
    a = confirm("Do you want to place another order?"); 

    (b==false) 
    document.write("Thank you for placing an order with us, " +username+ ".<br/>"); 
    document.write("The total order cost is $" +total+ "."); 

    </script> 
    </body> 
    </html> 
+4

我重新標記這是[標籤:JavaScript的] - Java和Javascript有一樣共同作爲一輛汽車和一個胡蘿蔔。例如, – corsiKa

+0

+1。 – denolk

回答

0

對於問題的第二部分,你必須在循環波紋管使用語句

a = confirm("Do you want to place another order?"); 
+0

我在哪裏把a = confirm(「你想要下另一個訂單?」);如果你想要求確認,那麼我是否會做另一個如果陳述,或者我把它放在else語句 – djw32592

+0

,該陳述應該在循環中。在你的情況下,do {}語句結束之前,關閉它。 – denolk

0

看起來你是不是調理輸出是否選擇有效產品的結果的部分。你需要像下面這樣的東西;

do{ 
    .... 

    productValid = false; 

    if (product == "hamburger") 
    { 
    price = bprice; 

    discount = .1; 
    productValid = true; 

    } 
    else if (product == "cheeseburger") 
    { 
    price = chprice; 

    discount = .05; 
productValid = true; 

    } 

    else if (product == "soda") 
    { 
    price = sprice; 

    discount = .07; 
productValid = true; 
    } 

    else if (product == "fries") 
    { 
    price = fprice; 

    discount = .15; 
productValid = true; 
    } 

    else{ 
     alert("Sorry, " +username+ " Your item not found."); 
    } 

if(productValid){ 
     cost=price*quantity 
     discount=price*discount*quantity 
     total=cost-discount 
    document.write("The cost of buying " +quantity+ " of " +product+ " is $" +cost+ ".<br/>"); 
    document.write("This discount for this purchase is $" +discount+ ".<br/>"); 
} 
else 
{ 
    document.write("No valid product selected<br>"); 
} 
a = confirm("Do you want to place another order?"); 

    }while(a==true) 
+0

另外,正如其他人所說的那樣,用於放置另一個訂單的confirm()應該發生在do-while內部,並且檢查的條件會是while(a == true),因爲如果它們需要再次循環選擇是。 – Bryan

+0

提示:用'productValid = true;'開始你的循環,如果找不到*,則將其更改爲false。這樣你就不用多次寫了。 –

+0

同意,但你可以在那一方面爭論。我通常更喜歡我最初的方法,因爲閱讀代碼時更有意義(產品在明確檢查之前無效),但需要花費額外的代碼。 – Bryan

0

如果用戶點擊cancel,那麼您正在重新循環該循環。更改while(a==false)while(a==true)或者乾脆while(a)

也把線

a = confirm("Do you want to place another order?"); 

在你的循環中的最後一行,而不是以後就行了。

你可以設置一個標誌,無功,讓你知道如果該項目被發現沒有,並檢查它在你的循環結束,以避免不存在的項目顯示數據:

<script type="text/javascript"> 
    /*Daniel Weiner, Fengpeng Yuan, Javascript 2, Nov 4,2011*/ 

    var username; 
    var bprice= 8; 
    var chprice= 9; 
    var sprice= 2; 
    var fprice= 4; 
    var price= 0; 
    var a; 
    var b; 
    var product= "hamburger, cheeseburger, soda, fries"; 
    var quantity =0; 
    var total; 
    var cost = 0; 
    var discount= 0; 
    var flag; 

    do{ 
    flag = true; //assume found unless otherwise 
    username =prompt("Welcome to Big Ben's Burgers. Please enter your name.", ""); 
    alert("Hello " + username+". Please look through our available products and services before placing your order.",""); 
    product=prompt("What do you want?",""); 
    quantity =1*prompt("How many of " +product+ " would you like?"); 

    if (product == "hamburger") 
    { 
    price = bprice; 

    discount = .1; 
    } 
    else if (product == "cheeseburger") 
    { 
    price = chprice; 

    discount = .05; 
    } 

    else if (product == "soda") 
    { 
    price = sprice; 

    discount = .07; 
    } 

    else if (product == "fries") 
    { 
    price = fprice; 

    discount = .15; 
    } 
    else{ 
     alert("Sorry, " +username+ " Your item not found."); 
     flag = false; 
    } 

    if(flag){ 
     cost=price*quantity 
     discount=price*discount*quantity 
     total=cost-discount 
     document.write("The cost of buying " +quantity+ " of " +product+ " is $" +cost+ ".<br/>"); 
     document.write("This discount for this purchase is $" +discount+ ".<br/>"); 
    } 
    a = confirm("Do you want to place another order?"); 
    }while(a); 
    alert('goodbye'); 
    </script>