设计一个能根据用户输入的商品编号和数量,计算总价并显示清单的程序。 商品编号:03,数量:5,商品名称:文件包,商品单价12.00,合计:60.00
时间: 2024-05-30 21:14:42 浏览: 15
好的,请输入商品编号和数量,我帮你计算总价和显示清单。
用户输入:03,5
计算结果:
商品名称:文件包
商品单价:12.00
数量:5
总价:60.00
清单如下:
商品名称 单价 数量 总价
文件包 12.00 5 60.00
请问还有其他需要我帮忙的吗?
相关问题
用javascript设计一个能根据用户输入的商品编号和数量,计算总价并显示清单的程序。
可以的,以下是代码示例:
<html>
<body>
<h1>商品清单</h1>
<label for="product_id">商品编号:</label>
<input type="number" id="product_id"><br><br>
<label for="quantity">数量:</label>
<input type="number" id="quantity"><br><br>
<button onclick="calculatePrice()">计算总价</button><br><br>
<label for="total_price">总价:</label>
<input type="text" id="total_price" readonly><br><br>
<script>
function calculatePrice() {
var product_id = document.getElementById("product_id").value;
var quantity = document.getElementById("quantity").value;
var price = 0;
// 根据商品编号计算价格
switch(product_id) {
case 1:
price = 10;
break;
case 2:
price = 20;
break;
case 3:
price = 30;
break;
default:
alert("请输入正确的商品编号!");
return;
}
// 计算总价并显示
var total_price = price * quantity;
document.getElementById("total_price").value = total_price;
}
</script>
</body>
</html>
用html设计一个能根据用户输入的商品编号和数量,计算总价并显示清单的程序。
<!DOCTYPE html>
<html>
<head>
<title>购物清单</title>
<meta charset="UTF-8">
</head>
<body>
<h1>购物清单</h1>
<form>
<label for="product-id">请输入商品编号:</label>
<input type="text" id="product-id" name="product-id"><br><br>
<label for="product-quantity">请输入商品数量:</label>
<input type="number" id="product-quantity" name="product-quantity"><br><br>
<input type="button" value="计算总价" onclick="calculatePrice()">
</form>
<br>
<h2>清单</h2>
<table id="shopping-list">
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品单价</th>
<th>商品数量</th>
<th>商品总价</th>
</tr>
</table>
<h2 id="total-price"></h2>
<script>
function calculatePrice() {
var productID = document.getElementById("product-id").value;
var productQuantity = document.getElementById("product-quantity").value;
var productList = [
{
id: "001",
name: "商品A",
price: 10
},
{
id: "002",
name: "商品B",
price: 20
},
{
id: "003",
name: "商品C",
price: 30
}
];
var product = productList.find(function(item) {
return item.id === productID;
});
if (product === undefined) {
alert("商品编号不存在,请重新输入!");
return;
}
var productName = product.name;
var productPrice = product.price;
var productTotalPrice = productPrice * productQuantity;
var table = document.getElementById("shopping-list");
var row = table.insertRow(-1);
var idCell = row.insertCell(0);
var nameCell = row.insertCell(1);
var priceCell = row.insertCell(2);
var quantityCell = row.insertCell(3);
var totalPriceCell = row.insertCell(4);
idCell.innerHTML = productID;
nameCell.innerHTML = productName;
priceCell.innerHTML = productPrice;
quantityCell.innerHTML = productQuantity;
totalPriceCell.innerHTML = productTotalPrice;
var totalPrice = 0;
for (var i = 1; i < table.rows.length; i++) {
totalPrice += parseInt(table.rows[i].cells[4].innerHTML);
}
document.getElementById("total-price").innerHTML = "总价:" + totalPrice;
}
</script>
</body>
</html>
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)