2011-02-13 61 views
0

我開始編程C,並提出了一個簡短的測驗程序。該程序要求用戶輸入他們想要回答的問題的數量。那麼問題的格式是相同的(#+#+# - #),但每次都會生成隨機數。我的問題是如何向用戶顯示他們在程序結束時得到的正確答案的數量?我知道你將要執行打印˚F語句來顯示它,但我不知道還有什麼如何顯示正確答案的數量?

#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 

回答

0
 
if(responce == answer){ 
     printf("\ncorrect\n"); 
     NumCorrect ++; 
} 
 
0

您有一個名爲NumCorrect變量,但你不使用它您while循環內。

你需要在while循環內部做些什麼,或許在if聲明中。 ;-)

1

在你的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_count在這裏,而不是NumCorrect,因爲你應該在你的命名保持一致;你所有的其他變量都是小寫,那麼你爲什麼選擇使NumCorrect標題容納?作爲常規編程學科的一部分,一致性非常重要。

+0

非常感謝我回答的問題和其他提示! – Arbin 2011-02-13 05:45:38