C语言 如何将错误答案算作错误答案?

  • 本文关键字:错误 答案 语言 c windows
  • 更新时间 :
  • 英文 :


在运行时显示答案是否正确,但即使是错误的,它仍然会被添加到正确答案的数量中。我要做什么或添加什么才能使它起作用?

#include<stdio.h>
int main(void) {
char choice;
int correctAnswer = 0, wrongAnswer = 0;
printf("Who developed C?n");
printf("A. Dennis Leary tC. Dennis RodmannB. Dennis Ritchie tD. Dennis Ruth");
printf("nAnswer: ");
scanf("%c", &choice);
switch (choice)
{
case 'A':
printf("Wrong Answer.n");
break;
case 'B':
printf("Correct Answer.n");
break;
case 'C':
printf("Wrong Answer.n");
break;
case 'D':
printf("Wrong Answer.n");
break;
default:
printf("Invalid Answern");

}
if ('B')
correctAnswer++;
else
wrongAnswer++;

printf("Number of Correct Answers: %dn", correctAnswer++);
printf("Number of Wrong Answers: %dn", wrongAnswer++);
}```

I expect the code to add the wrong answers to be added in the Number of Wrong Answers and it seems it recognized all answers as a correct answer.

this

if ('B')

总是为真。改为:

if (choice == 'B')

最新更新