printf后面的语句会连续输出两次
#include<stdio.h>#include<stdlib.h>#include<time.h>#defineExerNumber10;voidtest(){int...
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ExerNumber 10;
void test()
{
int algType=0;
int i;
int points=0;
int num1=0,num2=0;
int result=0;
int answer=0;
printf("现在开始计算,请看题:\n");
for(i=1;i<=10;i++)
{
srand((unsigned int)time(NULL));
num1=rand()%100;
num2=rand()%100;
algType=rand()%3+1;
switch(algType)
{
case 1:
result=num1+num2;
printf("%d+%d",num1,num2);
break;
case 2:
if(num1>num2)
{
printf("%d-%d",num1,num2);
result=num1-num2;
}
else
{
printf("%d-%d",num2,num1);
result=num2-num1;
}
break;
case 3:
result=num1*num2;
printf("%d*%d",num1,num2);
break;
}
printf("=?");
scanf("%d",&answer);
if(answer==result)
points++;
}
printf("你的成绩:\n");
switch(points)
{
case 10:
case 9: printf("优秀。");break;
case 8:
case 7: printf("良好。");break;
case 6:
case 5: printf("不错。");break;
default:printf("You should work hard!!");
}
}
void main()
{
char YesOrNo;
test();
while (1)
{
printf("\n你还想继续吗(y/n)?");
scanf("%c",&YesOrNo);
if(YesOrNo == 'n'||YesOrNo == 'N') exit(0);
if(YesOrNo == 'y'||YesOrNo == 'Y') test();
}
}
为什么输出时输出2个printf("\n你还想继续吗(y/n)?"); 展开
#include<stdlib.h>
#include<time.h>
#define ExerNumber 10;
void test()
{
int algType=0;
int i;
int points=0;
int num1=0,num2=0;
int result=0;
int answer=0;
printf("现在开始计算,请看题:\n");
for(i=1;i<=10;i++)
{
srand((unsigned int)time(NULL));
num1=rand()%100;
num2=rand()%100;
algType=rand()%3+1;
switch(algType)
{
case 1:
result=num1+num2;
printf("%d+%d",num1,num2);
break;
case 2:
if(num1>num2)
{
printf("%d-%d",num1,num2);
result=num1-num2;
}
else
{
printf("%d-%d",num2,num1);
result=num2-num1;
}
break;
case 3:
result=num1*num2;
printf("%d*%d",num1,num2);
break;
}
printf("=?");
scanf("%d",&answer);
if(answer==result)
points++;
}
printf("你的成绩:\n");
switch(points)
{
case 10:
case 9: printf("优秀。");break;
case 8:
case 7: printf("良好。");break;
case 6:
case 5: printf("不错。");break;
default:printf("You should work hard!!");
}
}
void main()
{
char YesOrNo;
test();
while (1)
{
printf("\n你还想继续吗(y/n)?");
scanf("%c",&YesOrNo);
if(YesOrNo == 'n'||YesOrNo == 'N') exit(0);
if(YesOrNo == 'y'||YesOrNo == 'Y') test();
}
}
为什么输出时输出2个printf("\n你还想继续吗(y/n)?"); 展开
2个回答
展开全部
因为输入输出流没清空,第一个printf之后scanf马上读入了一个值,于是再次循环
在printf("\n你还想继续吗(y/n)?");前加上一句fflush(stdin);来清空输入流就可以了
在printf("\n你还想继续吗(y/n)?");前加上一句fflush(stdin);来清空输入流就可以了
追问
scanf不是要我输入才能读入一个值的吗?
追答
因为你之前的输入有个换行符在缓冲区里,所以要清空它
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询