Noob在这里。我似乎无法解决"错位的其他"以下代码出错。此代码应收集和计算期限等级,并根据分数给出评论。任何帮助表示赞赏。
#include <conio.h>
#include <stdio.h>
main()
{
char name[20];
int exam, q1, q2, q3, ass, sw, att, avgq, CS, TG;
clrscr();
printf("Name: ");
gets(name);
printf("\nExam: ");
scanf("%d", &exam);
printf("\nQuiz #1: ");
scanf("%d", &q1);
printf("\nQuiz #2: );
scanf("%d",&q2);
printf("\nQuiz #3: ");
scanf("%d",&q3);
printf("\nAssignment: ");
scanf("%d",&ass);
printf("\nSeatwotk: ");
scanf("%d",&sw);
printf("\nAttendance: ");
scanf("%d",&att);
CS=(0.4*ass)+(0.4*sw)+(0.2*att); //class standing//
avgq=(q1+q2+q3)/3; //average quiz//
TG=(0.4*exam)+(0.3*avgq)+(0.3*CS); //term grade//
if(TG>=90)
printf("Term Grade: %d",TG);
printf("Remarks: EXCELLENT");
else if (TG>=80 && TG<=89)
printf("Term Grade: %d",TG);
printf("Remarks: SATISFACTORY");
else if (TG>=76 && TG<=79)
printf("Term Grade: %d",TG);
printf("Remarks: GOOD");
else if (TG==75)
printf("Term Grade: %d",TG);
printf("Remarks: PASSING");
else if (TG<74)
printf("Term Grade: %d",TG);
printf("Remarks: FAILED");
else
printf("Invalid Input. Try again");
getch();
return 0;
}
答案 0 :(得分:1)
所以不要担心美满!问题出在这里:
当声明if语句时,必须用大括号括起if语句的主体。如果不这样做,则只运行if语句下面的第一行。这是一个例子:
// here, both do something 1 and do something 2 are being run in the if statement
if (something) {
do something 1;
do something 2;
}
// here, only do something 1 will get run inside the if statement
if (something)
do something 1;
do something 2;
回到你的问题。如果if语句主体包含多于1行,则必须在if语句的代码周围放置大括号{}。
if (something)
do something 1;
do something 2;
else
do something 3;
相当于
if (something)
do something 1;
do something 2;
else do something 3;
这就是为什么你的else语句会抛出错误。其他每个人都必须有一个if。
答案 1 :(得分:0)
C ++不使用缩进来确定语句的结尾。如果你想要多个陈述,你需要大括号。
而不是:
if (a)
b;
c;
else
d;
使用:
if (a) {
b;
c;
} else {
d;
}
答案 2 :(得分:0)
如果你在if或else下有一行以上,那么它们必须包含在花括号中,如下所示:
if(TG>=90)
{
printf("Term Grade: %d",TG);
printf("Remarks: EXCELLENT");
}
else if (TG>=80 && TG<=89)
{
printf("Term Grade: %d",TG);
printf("Remarks: SATISFACTORY");
}