Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何显示正确答案的数量?_C_While Loop - Fatal编程技术网

C 如何显示正确答案的数量?

C 如何显示正确答案的数量?,c,while-loop,C,While Loop,我开始编写C语言程序,并提出了一个简短的测试程序。程序要求用户输入他们想要回答的问题数量。然后,问题的格式相同(#+#+#-#),但每次都会生成随机数。我的问题是如何向用户显示他们在程序结束时得到的正确答案的数量?我知道您必须执行printf语句才能显示它,但我不知道还有什么 #include <stdio.h> #include <time.h> #include <conio.h> #include <stdlib.h> main() { s

我开始编写C语言程序,并提出了一个简短的测试程序。程序要求用户输入他们想要回答的问题数量。然后,问题的格式相同(#+#+#-#),但每次都会生成随机数。我的问题是如何向用户显示他们在程序结束时得到的正确答案的数量?我知道您必须执行printf语句才能显示它,但我不知道还有什么

#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
main()
{

srand(time(NULL));

int NumQuestions = 0;
int responce = 0;
int loopcount = 0;
int answer = 0;
int NumCorrect = 0; // HOW TO GET THIS ???????????????????????????????

printf("\n\welcome to your math quize!\n ");

printf("\ntype the numer of questions you would like to answer: ");
scanf("%d", &NumQuestions); //number of questions. 

while(loopcount<NumQuestions){

int n1 = 0;
int n2 = 0; 
int n3 = 0; 
int n4 = 0;
n1 = rand()% 9 + 1;
n2 = rand()% 9 + 1; 
n3 = rand()% 9 + 1; 
n4 = rand()% 9 + 1; 
answer = n1 + n2 + n3 - n4;

              printf("\n%d + %d + %d - %d =", n1, n2, n3, n4);
              scanf("%d", &responce); // user answer

                          if(responce == answer)
                           printf("\ncorrect\n");

                           else
                           printf("\nincorrect\n");

loopcount++;
} //exit loop

printf("you got %d andswers correct!", NumCorrect); //????????????????????????????

getch();
} // end process 
#包括
#包括
#包括
#包括
main()
{
srand(时间(空));
int NumQuestions=0;
int responce=0;
int loopcount=0;
int-answer=0;
int NumCorrect=0;//如何获取此信息???????????????????????????????
printf(“\n \欢迎参加数学测验!\n”);
printf(“\n键入您想回答的问题数量:”;
scanf(“%d”,&NumQuestions);//问题数量。

while(loopcount您有一个名为
NumCorrect
的变量,但您没有在
while
循环中使用它


您需要在
while
循环中对它做些什么,可能在
if
语句中;-

在您的if语句中:

 if(responce == answer)
                       printf("\ncorrect\n");

                       else
                       printf("\nincorrect\n");
应首先添加大括号并正确设置其格式:

 if (responce == answer) {
     printf("\ncorrect\n");
 } else {
     printf("\nincorrect\n");
 }
那么你应该修正英语:

 if (response == answer) {
     printf("\ncorrect\n");
 } else {
     printf("\nincorrect\n");
 }
然后,您需要做的就是为正确的情况增加计数器:

 if (response == answer) {
     printf("\ncorrect\n");
     correct_count++;
 } else {
     printf("\nincorrect\n");
 }
另外,请注意,我在这里使用了
correct\u count
而不是
NumCorrect
,因为您的命名应该是一致的;所有其他变量都是小写的,所以为什么您选择将
NumCorrect
标题设置为cased?一致性作为常规编程规程的一部分非常重要

 if (response == answer) {
     printf("\ncorrect\n");
     correct_count++;
 } else {
     printf("\nincorrect\n");
 }