4、创建 2-4.htm1 文件,使用prompt 方法,分别输入你的姓名、各课程成绩,计算出总分、平均分(结果保留2位小数),输出结果参照如下。 任珍西的操作系统成绩为72分,unity游戏开发成绩为83分,框架库技术成绩为90分,总成绩为245分,平均分为81.67分。
时间: 2024-09-20 07:06:28 浏览: 50
要在`2-4.htm`文件中使用`prompt`方法获取用户的输入并计算总分和平均分,你可以按照以下步骤编写JavaScript代码:
```html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>成绩计算</title>
</head>
<body>
<h1>请输入你的成绩信息:</h1>
<!-- 获取姓名 -->
<p>姓名:</p>
<input type="text" id="name" placeholder="请输入姓名">
<!-- 获取各科成绩 -->
<p>操作系统成绩:</p>
<input type="number" id="os_score" placeholder="请输入分数">
<p>Unity游戏开发成绩:</p>
<input type="number" id="unity_score" placeholder="请输入分数">
<p>框架库技术成绩:</p>
<input type="number" id="framework_score" placeholder="请输入分数">
<button onclick="calculate()">计算</button>
<p id="result"></p>
<script>
function calculate() {
// 获取用户输入
const name = document.getElementById('name').value;
const osScore = parseInt(document.getElementById('os_score').value);
const unityScore = parseInt(document.getElementById('unity_score').value);
const frameworkScore = parseInt(document.getElementById('framework_score').value);
// 计算总分和平均分
const totalScore = osScore + unityScore + frameworkScore;
const averageScore = totalScore / 3.toFixed(2); // toFixed(2)保留两位小数
// 显示结果
document.getElementById('result').innerText = `${name}的成绩如下:\n总分:${totalScore}, 平均分:${averageScore}`;
}
</script>
</body>
</html>
```
当你在页面上填写完姓名和课程成绩,然后点击“计算”按钮,就会看到结果显示为:
任珍西的操作系统成绩为72分,unity游戏开发成绩为83分,框架库技术成绩为90分,总成绩为245分,平均分为81.67分。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![whl](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)