当你进行过了scanf函数后, 在第二次scanf函数时, 由于缓存区里依然存着第一次结束时送入的回车符,在第二次时没有读入就直接把回车符送入了,所以没有真正起到读入数据的作用.只要在合适的地方加入fflush(stdin)清空缓存区,再运行scanf时就可以了.
#include<math.h>
#include <stdlib.h>
#include<stdio.h>
char judge(int try,int answer){
if(try==answer){
return 'e';
}
else return 'u';
}
void main(){
int order=0,get=1;
int try=0;
int level;
int bits;
int answer;
char result='n';
int length;
char judgeIf;
printf("Game begins!/nWelcome to the number guessing game!/nThis is my first biggest program./nHope you like it./nIt is edited by c-resource./n");
printf("Now it begins./n");
do{
printf("Choose the level 3-10 :");
scanf("%d",&level);
printf("Set the length of the number 3-5 bits :");
scanf("%d",&bits);
answer=random(bits);
printf("Now the number is ready.You can try it now./nInput your try :");
while(order<level){
printf("/nTime %d : (%d)",++order,answer);
scanf("%d",&try);
if(judge(try,answer)=='e'){
printf("/nYou are right.Congratulations!");
break;
}
else {
if(order==level){
printf("You are failed.");
}
else printf("Try it again.");
}
}
if(get==1){
printf("Are you want to play it again?");
scanf("%c",&result);
printf("/nYou are so clever now with the help of my software.");
}
}
while(result=='y');
}