使用javascript语言,用表单输入10本书的价格,然后显示这10本书的最高价格、最低jiage、和平均价格
时间: 2024-05-06 20:15:38 浏览: 142
可以使用如下代码实现:
```html
<!DOCTYPE html>
<html>
<head>
<title>计算书籍价格</title>
</head>
<body>
<form>
<label for="book1">书1价格:</label>
<input type="number" id="book1" required><br>
<label for="book2">书2价格:</label>
<input type="number" id="book2" required><br>
<label for="book3">书3价格:</label>
<input type="number" id="book3" required><br>
<label for="book4">书4价格:</label>
<input type="number" id="book4" required><br>
<label for="book5">书5价格:</label>
<input type="number" id="book5" required><br>
<label for="book6">书6价格:</label>
<input type="number" id="book6" required><br>
<label for="book7">书7价格:</label>
<input type="number" id="book7" required><br>
<label for="book8">书8价格:</label>
<input type="number" id="book8" required><br>
<label for="book9">书9价格:</label>
<input type="number" id="book9" required><br>
<label for="book10">书10价格:</label>
<input type="number" id="book10" required><br>
<input type="button" value="计算" onclick="calculate()"><br><br>
<label for="maxPrice">最高价格:</label>
<input type="text" id="maxPrice" disabled><br>
<label for="minPrice">最低价格:</label>
<input type="text" id="minPrice" disabled><br>
<label for="averagePrice">平均价格:</label>
<input type="text" id="averagePrice" disabled><br>
</form>
<script>
function calculate() {
// 获取输入框的值
var book1 = parseFloat(document.getElementById("book1").value);
var book2 = parseFloat(document.getElementById("book2").value);
var book3 = parseFloat(document.getElementById("book3").value);
var book4 = parseFloat(document.getElementById("book4").value);
var book5 = parseFloat(document.getElementById("book5").value);
var book6 = parseFloat(document.getElementById("book6").value);
var book7 = parseFloat(document.getElementById("book7").value);
var book8 = parseFloat(document.getElementById("book8").value);
var book9 = parseFloat(document.getElementById("book9").value);
var book10 = parseFloat(document.getElementById("book10").value);
// 计算最高价格、最低价格、平均价格
var maxPrice = Math.max(book1, book2, book3, book4, book5, book6, book7, book8, book9, book10);
var minPrice = Math.min(book1, book2, book3, book4, book5, book6, book7, book8, book9, book10);
var averagePrice = (book1 + book2 + book3 + book4 + book5 + book6 + book7 + book8 + book9 + book10) / 10;
// 将结果显示在输入框中
document.getElementById("maxPrice").value = maxPrice;
document.getElementById("minPrice").value = minPrice;
document.getElementById("averagePrice").value = averagePrice.toFixed(2);
}
</script>
</body>
</html>
```
通过上面的代码,我们可以实现一个表单,让用户输入10本书的价格,然后通过JavaScript计算这些书的最高价格、最低价格和平均价格,并将结果显示在页面上。
阅读全文