使用Java 】 某同学参加了高数、英语、Java 语言、线性代数和物理 5 门课程的考成绩分别为 70、86、77、90 和82。请用数组来存放其成绩,并计算 20个同学学号依次的最均分。
时间: 2024-05-13 18:18:21 浏览: 97
可以使用以下Java代码来实现:
```java
public class ScoreCalculator {
public static void main(String[] args) {
int[] scores = {70, 86, 77, 90, 82};
int sum = 0;
for (int i = 0; i < scores.length; i++) {
sum += scores[i];
}
double average = (double) sum / scores.length;
System.out.println("平均分为:" + average);
}
}
```
输出结果为:
```
平均分为:81.0
```
这里定义了一个整型数组 `scores` 存放每门课程的成绩,使用循环遍历数组并累加每门课程的成绩,最后计算平均分并输出。
相关问题
python考试成绩.xls包含了年纪159个同学高等数学考试成绩,和某年级288个同学的线性代数成绩,用xlrd库读取两个成绩并显示
在Python中,我们可以使用`xlrd`库来读取Excel文件中的数据。首先,你需要安装这个库,可以使用pip命令安装:
```bash
pip install xlrd
```
然后,你可以编写以下代码来分别读取两个成绩文件(假设文件名分别为`exam_math.xls`和`exam_linear_algebra.xls`):
```python
import xlrd
# 高等数学成绩文件
math_scores_file = 'exam_math.xls'
math_data = xlrd.open_workbook(math_scores_file)
math_sheet = math_data.sheet_by_index(0) # 假设第一个工作表是成绩表
math_student_count = math_sheet.nrows - 1 # 除第一行标题外的行数
math_scores = [math_sheet.cell_value(rowx, 0) for rowx in range(1, math_student_count + 1)] # 第一列分数
# 线性代数成绩文件
linear_algebra_scores_file = 'exam_linear_algebra.xls'
linear_algebra_data = xlrd.open_workbook(linear_algebra_scores_file)
linear_algebra_sheet = linear_algebra_data.sheet_by_index(0)
algebra_student_count = linear_algebra_sheet.nrows - 1
linear_algebra_scores = [linear_algebra_sheet.cell_value(rowx, 0) for rowx in range(1, algebra_student_count + 1)]
# 显示成绩
print(f"高等数学成绩有 {len(math_scores)} 人,平均分大约是:{sum(math_scores)/len(math_scores)}")
print(f"线性代数成绩有 {len(linear_algebra_scores)} 人,平均分大约是:{sum(linear_algebra_scores)/len(linear_algebra_scores)}")
```
这段代码会读取每个成绩文件的第一个工作表,并提取出所有学生的成绩。注意实际操作时需要根据你的文件结构和数据位置调整索引。
python代码实现高数线代考试成绩.xls包含了某年级159个同学的高等数学考试成绩,和某年级288个同学的线性代数考试成绩。使用xlrd库读取这两个成绩,并显示出来
以下是使用xlrd库读取高数线代考试成绩.xls文件中的高等数学和线性代数考试成绩,并将其显示出来的Python代码实现:
```python
import xlrd
# 打开Excel文件
workbook = xlrd.open_workbook('高数线代考试成绩.xls')
# 获取高等数学成绩表格
math_sheet = workbook.sheet_by_name('高数成绩')
# 获取线性代数成绩表格
linear_algebra_sheet = workbook.sheet_by_name('线代成绩')
# 获取高等数学成绩
math_scores = []
for i in range(1, math_sheet.nrows):
math_scores.append(math_sheet.cell_value(i, 1))
# 获取线性代数成绩
linear_algebra_scores = []
for i in range(1, linear_algebra_sheet.nrows):
linear_algebra_scores.append(linear_algebra_sheet.cell_value(i, 1))
# 显示高等数学成绩
print('高等数学成绩:')
for i, score in enumerate(math_scores):
print('学生{}的成绩为:{}'.format(i+1, score))
# 显示线性代数成绩
print('线性代数成绩:')
for i, score in enumerate(linear_algebra_scores):
print('学生{}的成绩为:{}'.format(i+1, score))
```
运行上述代码,即可输出高等数学和线性代数考试成绩。注意,需要将代码中的文件路径修改为实际文件的路径。
阅读全文
相关推荐
















