编写c语言程序从键盘输入若干学生的成绩,并输出在屏幕上(要求每行输出3个成绩),用- 1结束循环的执行用while循环实现。
时间: 2023-05-18 19:05:34 浏览: 72
```c
#include <stdio.h>
int main() {
int score, count = 0;
printf("请输入学生成绩,以-1结束:\n");
scanf("%d", &score);
while (score != -1) {
printf("%d ", score);
count++;
if (count % 3 == 0) {
printf("\n");
}
scanf("%d", &score);
}
return 0;
}
```
说明:
1. 定义变量`score`表示输入的成绩,`count`表示已经输入的成绩个数,初始值为0。
2. 输出提示信息,让用户输入学生成绩。
3. 使用`while`循环,当输入的成绩不为-1时,执行循环体。
4. 在循环体中,输出输入的成绩,并将`count`加1。
5. 判断`count`是否为3的倍数,如果是,输出换行符。
6. 再次让用户输入成绩,更新`score`的值。
7. 循环结束后,返回0表示程序正常结束。
相关问题
用C语言编写程序: 从键盘上输入若干学生的成绩,以-1作为结束标志,统计并输出其中的最高成绩及平均成绩
这个问题可以通过以下步骤解决:
1. 定义变量,包括成绩变量、计数器变量、总成绩变量、最高成绩变量。
2. 使用while循环,输入学生的成绩,每输入一个成绩,就将其加到总成绩变量中,并将计数器变量加1。
3. 在循环中,判断输入的成绩是否大于最高成绩变量,如果是,则将其赋值给最高成绩变量。
4. 当输入-1时,跳出循环,计算平均成绩。
5. 输出最高成绩和平均成绩。
下面是代码实现:
```
#include <stdio.h>
int main() {
float score, total = 0, avg;
int count = 0, max = 0;
printf("请输入学生成绩:\n");
scanf("%f", &score);
while (score >= 0) {
total += score;
count++;
if (score > max) {
max = score;
}
scanf("%f", &score);
}
if (count > 0) {
avg = total / count;
printf("最高成绩为:%d\n平均成绩为:%.2f\n", max, avg);
} else {
printf("没有输入成绩!\n");
}
return 0;
}
```
用C语言编写一个从键盘输入若干个学生的成绩,当成绩小于0时结束输入。计算出平均成绩,并输出不及格的成绩和人数的程序
好的,以下是您需要的C语言代码:
```c
#include <stdio.h>
int main() {
int score, sum = 0, count = 0, fail_count = 0;
float average;
printf("请输入学生成绩(若干个),以负数结束:\n");
while (1) {
scanf("%d", &score);
if (score < 0) {
break;
}
sum += score;
count++;
if (score < 60) {
fail_count++;
}
}
if (count == 0) {
printf("没有输入任何成绩!\n");
} else {
average = (float)sum / count;
printf("平均成绩为:%.2f\n", average);
if (fail_count > 0) {
printf("不及格的成绩及人数为:%d\n", fail_count);
} else {
printf("没有不及格的成绩!\n");
}
}
return 0;
}
```
注:代码中的`while(1)`表示一个死循环,只有当输入的成绩小于0时才会通过`break`语句跳出循环。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)
![](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)