C 为什么我的代码在运行时输出0?出了什么问题,我该如何解决?
这是我的代码,用来显示使用for循环的数组的和和和平均值,但当我运行它时,它只为和和平均值输出0C 为什么我的代码在运行时输出0?出了什么问题,我该如何解决?,c,arrays,for-loop,output,average,C,Arrays,For Loop,Output,Average,这是我的代码,用来显示使用for循环的数组的和和和平均值,但当我运行它时,它只为和和平均值输出0 #include <stdio.h> int main (void){ float grades[12] = {85.0, 77.0, 15.0, 100.0, 90.0, 98.0, 62.0, 84.0, 86.0, 70.0, 100.0, 99.0}; int counter; float average; float sum = 0.0;
#include <stdio.h>
int main (void){
float grades[12] = {85.0, 77.0, 15.0, 100.0, 90.0, 98.0, 62.0, 84.0, 86.0, 70.0, 100.0, 99.0};
int counter;
float average;
float sum = 0.0;
for(counter = 0; counter == 12; counter++){
sum = sum + grades[counter];
}
average = sum/12;
printf("The sum of the grades is: %f \n", sum);
printf("The average of the grades are: %f \n", average);
system("pause");
}
如果条件为假,则停止。您的条件counter==12在第一次迭代时为false,因此循环永远不会运行。for的条件为false时立即停止。您的条件counter==12在第一次迭代时为false,因此循环永远不会运行。for循环为:forinit;虽然增量
请注意,这只是暂时的,直到
您的循环永远不会运行:
for(counter = 0; counter == 12; counter++){
因为0永远不等于12。for循环是:forinit;虽然增量
请注意,这只是暂时的,直到
您的循环永远不会运行:
for(counter = 0; counter == 12; counter++){
因为0永远不等于12。您的for循环是错误的。试一试
for(counter = 0; counter < 12; counter++) {
...
}
你的for循环是错误的。试一试
for(counter = 0; counter < 12; counter++) {
...
}