1. 程式人生 > >設計程式:利用迴圈編寫程式,幫助小學生練習10以內加減法

設計程式:利用迴圈編寫程式,幫助小學生練習10以內加減法

#include <stdio.h>
#include "time.h"
#include "stdlib.h"


int main()
{
    int iNumber1=0,iNumber2=0;
    int iRightNum=0;
    const int cTotalNum=10;
    
    srand((int)time(0));
    for (int n=0; n<cTotalNum; n++) {
        int iResult=-1;
        iNumber1=rand()%10+1;
        iNumber2=rand()%10+1;
        printf("Question NO.%d\n",(n+1));
        printf("%d+%d=",iNumber1,iNumber2);
        scanf("%d",&iResult);
        
        if (iResult!=iNumber1+iNumber2) {
            printf("Wrong!\n");
        }
        else{
            printf("Right!\n");
            iRightNum++;
        }
    }
    printf("You have done %d questions, %d Right %d Wrong.\n",cTotalNum,iRightNum,(cTotalNum-iRightNum));
    
}