首先,我再试一次'部分应该回到原来的学生人数,而不是转移到下一个学生。我认为我使用错误的循环,但不知道如何解决它。我尝试将循环放在我的'之外(i = 1; i< 11; i ++)',但它没有用。
其次,我的min和avg工作正常,但不是最大。它一直给我一个随机数,我不知道为什么。
非常感谢你。
#include<stdio.h>
void main() {
int i,a[10],max=a[0],min=a[0],avg,sum=0;
printf("please input student score one at a time.\n");
for (i=1;i<11;i++) {
printf("Student %d : ",i);
scanf("%d",&a[i]);
while (a[i] > 100 || a[i] < 0) {
printf("Try again!\n");
i-1//im trying to keep the same student count after 'try again'
break;
}
if (max < a[i]) max = a[i];
if (min > a[i]) min = a[i];
sum+=a[i];
}
avg=sum/10;
printf("\n***************final result ***************\n");
printf(" The maximum score is %d\n",max);//only my max is broken. min & avg works fine
printf(" The minimum score is %d\n",min);
printf(" The average score is %d",avg);
getch();
}
答案 0 :(得分:1)
请试试这个
#include <stdio.h>
int main() {
int i = 0;
int a[10];
int max = -1;
int min = 101;
int avg = 0;
int sum = 0;
printf("please input student score one at a time.\n");
while(i < 10) {
printf("Student %d : ",i);
scanf("%d", &a[i]);
if(a[i] > 100 || a[i] < 0) {
printf("Try again!\n");
continue;
}
if (max < a[i]) max = a[i];
if (min > a[i]) min = a[i];
sum += a[i];
i++;
}
avg = sum/10;
printf("\n***************final result ***************\n");
printf(" The maximum score is %d\n",max);//only my max is broken. min & avg works fine
printf(" The minimum score is %d\n",min);
printf(" The average score is %d",avg);
return 0;
}
答案 1 :(得分:0)
看起来像家庭作业,所以我只提供如何解决问题的提示
希望这对您的问题有所帮助。
答案 2 :(得分:0)
#include<stdio.h>
void main() {
int i,a[10],max=a[0],min=a[0],avg,sum=0;
printf("please input student score one at a time.\n");
for (i=1;i<11;i++) {
printf("Student %d : ",i);
scanf("%d",&a[i]);
if (a[i] > 100 || a[i] < 0) {
printf("Try again!\n");
i--;
continue;
}
if (max < a[i]) max = a[i];
if (min > a[i]) min = a[i];
sum+=a[i];
}
avg=sum/10;
printf("\n***************final result ***************\n");
printf(" The maximum score is %d\n",max);
printf(" The minimum score is %d\n",min);
printf(" The average score is %d",avg);
}
中进行测试
答案 3 :(得分:-3)
感谢大家的帮助。我的代码现在看起来更好。我拿出了&#39; = [0]&#39;从最小和最大,并且改变了&#39;而&#39;到&#39;如果&#39;与--i。如果我需要更改,请告诉我。
#include<stdio.h>
void main() {
int i,a[10],max,min,avg,sum=0;
printf("please input student score one at a time.\n");
for (i=1;i<11;i++) {
printf("Student %d : ",i);
scanf("%d",&a[i]);
if (a[i] > 100 || a[i] < 0) {
printf("Try again!\n");
--i;
continue;
}
if (max < a[i]) max = a[i];
if (min > a[i]) min = a[i];
sum+=a[i];
}
avg=sum/10;
printf("\n***************final result ***************\n");
printf(" The maximum score is %d\n",max);
printf(" The minimum score is %d\n",min);
printf(" The average score is %d",avg);
getch();
}