如何将错误答案计为错误答案?

huangapple go评论61阅读模式
英文:

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&lt;stdio.h&gt;

int main(void) {
	char choice;
	int correctAnswer = 0, wrongAnswer = 0;

	printf(&quot;Who developed C?\n&quot;);
	printf(&quot;A. Dennis Leary \tC. Dennis Rodman\nB. Dennis Ritchie \tD. Dennis Ruth&quot;);
    printf(&quot;\nAnswer: &quot;);
	scanf(&quot;%c&quot;, &amp;choice);

	switch (choice)
	{
	case &#39;A&#39;:
		printf(&quot;Wrong Answer.\n&quot;);
		break;
	case &#39;B&#39;:
		printf(&quot;Correct Answer.\n&quot;);
		break;
	case &#39;C&#39;:
		printf(&quot;Wrong Answer.\n&quot;);
		break;
	case &#39;D&#39;:
		printf(&quot;Wrong Answer.\n&quot;);
		break;
	default:
		printf(&quot;Invalid Answer\n&quot;);
		
	}
	if (&#39;B&#39;)
		correctAnswer++;
	else
		wrongAnswer++;
	
	printf(&quot;Number of Correct Answers: %d\n&quot;, correctAnswer++);
	printf(&quot;Number of Wrong Answers: %d\n&quot;, 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 (&#39;B&#39;)

is always true. Change it to:

     if (choice == &#39;B&#39;)

</details>



huangapple
  • 本文由 发表于 2023年1月6日 13:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定