编写程序:从键盘上输入若干学生的成绩,统计并输出最高成绩和最低成绩,当输入负数时结束输入
时间: 2024-06-15 08:09:19 浏览: 24
编写这个程序可以分为以下几个步骤:
1. 创建一个空的列表,用于存储学生的成绩。
2. 使用一个循环,从键盘上逐个输入学生的成绩,并将其添加到列表中。当输入负数时,结束输入。
3. 使用内置函数`max()`和`min()`分别找到列表中的最高成绩和最低成绩。
4. 输出最高成绩和最低成绩。
下面是一个示例代码:
```python
scores = [] # 创建一个空的列表
while True:
score = float(input("请输入学生的成绩(输入负数结束):"))
if score < 0:
break
scores.append(score) # 将成绩添加到列表中
max_score = max(scores) # 找到最高成绩
min_score = min(scores) # 找到最低成绩
print("最高成绩:", max_score)
print("最低成绩:", min_score)
```
相关问题
用iostream编写c++程序从键盘上输入若干学生的成绩,统计并输出最高成绩和最低成绩,当输入负数时结束输入
以下是C知道的回答:
可以使用以下代码实现:
```c
#include <stdio.h>
int main() {
int score, max_score = -1, min_score = 101;
printf("请输入学生的成绩:\n");
while (1) {
scanf("%d", &score);
if (score < 0) {
break;
}
if (score > max_score) {
max_score = score;
}
if (score < min_score) {
min_score = score;
}
}
printf("最高成绩为:%d\n", max_score);
printf("最低成绩为:%d\n", min_score);
return 0;
}
```
这个程序使用了一个 while 循环来不断读入学生的成绩,当输入负数时结束输入。在循环中,使用 if 语句来统计最高成绩和最低成绩。最后,使用 printf 函数输出结果。
希望这个回答能够帮助到你!
编写一个c++程序从键盘上输入若干学生的成绩,统计并输出最高成绩和最低成绩,当输入负数时结束输入
以下是C知道的回答:
您好!以下是您提出的问题的答案:
```c
#include <stdio.h>
int main() {
int score, max_score = -1, min_score = 101;
printf("请输入学生的成绩:\n");
while (1) {
scanf("%d", &score);
if (score < 0) {
break;
}
if (score > max_score) {
max_score = score;
}
if (score < min_score) {
min_score = score;
}
}
printf("最高成绩:%d\n", max_score);
printf("最低成绩:%d\n", min_score);
return 0;
}
```
这是一个简单的C程序,可以从键盘上输入若干学生的成绩,统计并输出最高成绩和最低成绩。当输入负数时,程序会结束输入。