我的C语言学习笔记<三>
通过输入两个加数给学生出一道加法运算题,如果输入答案正确,则显示“Right!”,否则提示重做,显示“Not correct! Try again!”,最多给三次机会,如果三次仍未做对,则显示“Not correct! You have tried three times! Test over!”,程序结束。
#include <stdio.h>
#include <conio.h>
main()
{
int a,b,sum,k,i=1;
scanf("%d,%d",&a,&b);
printf("%d+%d=",a,b);
scanf("%d",&sum);
while(i<3&&sum!=a+b)
{
printf("input again");
scanf("%d",&sum);
if(sum==a+b)
{printf("right");break;}
else
{
i++;
}
}
if(i>=3) {printf("Not correct! You have tried three times! Test over!"); }
getch();
}
解法二
#include <stdio.h>
main()
{
int a,b,sum,i=0,k;
printf("shu ru liang ge shu:\n");
scanf("%d%d",&a,&b);
printf("%d+%d=\n",a,b);
sum=a+b;
do
{ i++;
scanf("%d",&k);
if(k==sum) {printf("right\n");break;}
if(i>2)
{printf("Not correct! You have tried three times! Test over!");break;}
if(k!=sum) printf("Not correct! Try again!\n");
}
while(i<=2);
getch();
}
解法三
#include <stdio.h>
#include <conio.h>
main()
{
int a,b,sum,k,i=0;
scanf("%d,%d",&a,&b);
printf("%d+%d=",a,b);
scanf("%d",&sum);
for(i=0;i<3;i++)
{
if(sum==a+b)
{printf("right");break;}
else
{
printf("input again");
scanf("%d",&sum);
}
}
if(i>=3) {printf("Not correct! You have tried three times! Test over!"); }
getch();
}