用if while语句编写: 输入若干学生成绩,以负数结束输入。计算所有学生的平均成绩并统计成绩在80到89分之间的人数。 注意:语言选DEV C++。 输入格式: 在一行输入几个百分制实型成绩,数据之间以一个空格间隔。输入负数结束输入。 输出格式: 在一行中按照“ave=平均分,n=人数”的顺序输出结果,其中平均分要求保留两位小数,人数是正整数。
时间: 2023-05-30 08:04:04 浏览: 236
代码如下:
```c
#include <stdio.h>
int main()
{
float score, sum = 0;
int count = 0, num = 0; // count记录输入的成绩个数,num记录80到89分之间的人数
printf("请输入学生成绩,以负数结束输入:\n");
scanf("%f", &score);
while (score >= 0) // 当输入负数时结束输入
{
sum += score;
count++;
if (score >= 80 && score <= 89)
{
num++;
}
scanf("%f", &score);
}
float ave = sum / count; // 计算平均成绩
printf("ave=%.2f,n=%d", ave, num);
return 0;
}
```
运行截图:
![运行截图](https://cdn.luogu.com.cn/upload/image_hosting/ed2e5wzj.png)
相关问题
用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++或Java。以下是使用C++语言的一个简单示例程序:
```cpp
#include <iostream>
using namespace std;
int main() {
int maxScore = 0; // 用于存储最高成绩
int score; // 用于存储当前输入的成绩
cout << "请输入学生的成绩,输入负数结束:" << endl;
while (true) {
cin >> score; // 从键盘读取成绩
if (score < 0) {
// 如果输入的是负数,结束循环
break;
}
if (score > maxScore) {
// 更新最高成绩
maxScore = score;
}
}
cout << "最高成绩是:" << maxScore << endl;
return 0;
}
```
这段代码的工作流程如下:
1. 程序开始运行后,声明一个变量 `maxScore` 来记录最高成绩,并初始化为0。
2. 使用一个 `while` 循环来不断从键盘接收用户输入的成绩。
3. 如果输入的成绩是负数,则使用 `break` 语句跳出循环。
4. 如果输入的成绩大于当前记录的最高成绩,则更新 `maxScore` 的值为该成绩。
5. 循环结束后,输出记录的最高成绩。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.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)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)