我正在尝试制作一个简单的杂货清单,该清单将提示输入商品和价格,而没有数组或仅使用循环的函数。到目前为止,我无法索要价格。这是代码的一部分。
var count = 0;
var groceryItems = ("Enter the grocery items (or DONE to exit)", "")
while (groceryItems != "DONE") {
count++
groceryItems = prompt("Enter the grocery items (or DONE to exit)", "")
}
我如何要求用户输入的每件商品的价格?
答案 0 :(得分:1)
我的意思是您必须将他们的答案存储在某个地方,对吗? 但是,如果您只希望答案是开放的,并一遍又一遍地存储它们输入的内容...
var count = 0;
var groceryItems = [];
var food=true;
//get rid out the count<5 if you want the user to be allowed to enter food forever
while (food && count<5) {
count++
food= prompt("Enter the grocery items (or Cancel to exit)","");
if(food){
var price=prompt("Enter the price (or Cancel to exit)","");
groceryItems.push(food+" $"+price);
}
}
//when you hit cancel all the entered items will be alerted.
alert(groceryItems.join(','));
而且,这可能不应该是面向公众的用户界面设计。