當你進行過了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');
}