我正在创建一个程序,让我输入 1-50 的 6 个年级,并让它给我总分和分数的百分比。我遇到的唯一麻烦是,当 if 循环激活时,它不会返回到上一个输入,而是跳到下一个输入。如何使其返回到之前的输入以便可以重新输入?
#include <stdio.h>
int main() {
int percent;
int totalscore;
const int score[5];
for (int grades = 1; grades <= 6; grades++) {
printf("please enter the grade for assignment #%d: ", grades);
scanf("%d", &score[grades]);
if (score > 0 , score > 51);
printf("Please enter a number between 1 and 50\n");
}
totalscore = score[1] + score[2] + score[3] + score[4] + score[5] + score[6];
printf("Over 6 assignments you earned %d points out of a possible 300 points!\n", totalscore);
percent = totalscore / 3 ;
printf("Your grade is %d percent.", percent);
return 0;
}
在这段代码中,您犯了两个错误,首先是您使用了分号“;”在“if”语句的末尾,其次您在“if”条件中使用了“,”,这是不正确的。 您可以看到下面的更正 -:
如果(得分<=0 || score>51) printf("请输入1到50之间的数字 ”);