英文:
How do I make the wrong answer count as wrong answer?
问题
#include <stdio.h>
int main(void) {
char choice;
int correctAnswer = 0, wrongAnswer = 0;
printf("Who developed C?\n");
printf("A. Dennis Leary \tC. Dennis Rodman\nB. 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");
correctAnswer++;
break;
case 'C':
printf("Wrong Answer.\n");
break;
case 'D':
printf("Wrong Answer.\n");
break;
default:
printf("Invalid Answer\n");
}
if (choice != 'B')
wrongAnswer++;
printf("Number of Correct Answers: %d\n", correctAnswer);
printf("Number of Wrong Answers: %d\n", wrongAnswer);
return 0;
}
代码已经被修正,现在会根据选择的答案来增加正确或错误答案的计数。
英文:
In running, it displays whether the answer is wrong or right, but even when it is wrong it is still get added into the Number of Correct Answers. What do I do or add to make it work?
#include<stdio.h>
int main(void) {
char choice;
int correctAnswer = 0, wrongAnswer = 0;
printf("Who developed C?\n");
printf("A. Dennis Leary \tC. Dennis Rodman\nB. 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 Answer\n");
}
if ('B')
correctAnswer++;
else
wrongAnswer++;
printf("Number of Correct Answers: %d\n", correctAnswer++);
printf("Number of Wrong Answers: %d\n", 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.
</details>
# 答案1
**得分**: -1
这部分翻译如下:
这个
如果('B')
总是为真。将其更改为:
如果(choice == 'B')
<details>
<summary>英文:</summary>
this
if ('B')
is always true. Change it to:
if (choice == 'B')
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论