<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p id="comm"></p>
<script type="text/javascript">
var hosh = [];
hosh["青菜"] = 1.2;
hosh["牛肉"] = 20;
hosh["猪肉"] = 30;
hosh["苹果"] = 6;
hosh["牙刷"] = 10;
hosh["牙膏"] = 16;
var Price_arr = [];
var Commod = [];
var sum = 0;
var eixt = "over";
for(var i = 0; i < 9999; i++) {
var commodit = prompt("请输入您所购买的商品");
if(hosh.indexOf(commodit)){//用来检测输入的商品是否在超市的数据中存在
alert("您输入的商品名称有误")
}
if(commodit == eixt) {
alert("开始计算总金额");
break;
} else if(commodit != eixt) {
Commod[i] = commodit;//用来存放输入商品的名称
Price_arr[i] = hosh[commodit];//用来存放下标是客户输入的商品的价格
}
}
document.getElementById("comm").innerText = "您购买的商品如下:";
for(var j = 0; j <= Price_arr.length - 1; j++) {
//便利商品名称和单价
document.write(Commod[j]);
document.write(Price_arr[j]);
document.write("<br/>")
sum = Price_arr[j] + sum;//计算总金额
}
document.write("您的总消费为:" + sum);
</script>
</body>
超市收银问题
最新推荐文章于 2020-12-21 18:41:09 发布