gpt4 book ai didi

c - 为什么我的循环没有在我预期的时候退出?

转载 作者:行者123 更新时间:2023-11-30 18:38:29 25 4
gpt4 key购买 nike

我目前正在做一些 C 编程练习题,但我遇到了一个我不太明白如何完成的问题。其过程如下:

Write a C program that will prompt the user to enter data about student
marks. After all the marks are entered, your program must print a short
report showing the minimum and maximum marks (along with the ID of the students
receiving those marks) and the average mark. The data will include student
numbers (integers) and marks (floating point). Here's an example of what an
interaction with your program might look like. The program's output is in
boldface and the user's input is not.

--------------------------

student number (0 to stop): 1234
mark: 72.5
student number (0 to stop): 2345
mark: 63.47
student number (0 to stop): 67298764
mark: 86
student number (0 to stop): 0

Lowest mark: 63.47 (student 2345)
Highest mark: 86 (student 67298764)
Average mark: 73.99

这是我到目前为止所想到的:

#include<stdio.h>
#include <stdlib.h>
int main()
{
int marks[5];
int i;

for(i=0;i<5;i++)
{
printf("student number (0 to stop): ");
scanf("%d\n", marks + i);
if (marks[i] == 0) {
break;
}



}

printf("\nEntered values:\n");
for(i=0;i<5;i++)
{
printf("%d\n",*marks);
}

return 0;

}我只是想让程序遵循在用户输入不起作用的 0 后退出循环的要求。我正在尝试一步一步地进行,但似乎我一开始就失败了...非常感谢任何帮助,谢谢。

最佳答案

您使用的是单个等于=,它是赋值运算符,而不是== 比较运算符。

关于c - 为什么我的循环没有在我预期的时候退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33816273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com