![]() |
#2
lin51616782020-04-02 18:06
|
按着书上的步骤做一个关于考验记忆力的小游戏,大概就是显示一串数字几秒钟,然后数字消失不见,用户凭着记忆力把数字输入进去,对了继续玩,错了显示分数并选择要不要继续玩。
运行程序后,本该显示1秒的数字都没有显示,过了1秒就直接提示用户输入数字,还有输入会跳过,因为至少是两个数字,所以每输入一个数字后要按一下空格再输其他数字,但是输入了两个后最后那个问用户是否继续的scanf就被跳过了,我在输入数字的那个scanf后面加了getchar函数的
我检查了半天没发现有什么问题,我自己也想不明白...大家帮帮忙,谢谢!!
只有本站会员才能查看附件,请 登录
他不会显示需要输入的数字,最后询问那个也被跳过了
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<time.h>
#include<stdbool.h>
int main(void)
{
const unsigned int delay=1;
char play_again='Y';
int number=0;//user's input;
int tries=0;//attempt
int digits=0;// number of digits in sequence
time_t seed=0;//seed value for random number sequence
time_t wait_start=0;// stores current time
bool correct=true;
int score=0;
int total_digits=0;
int game_time=0;
clock_t start_time=0;
printf("\nWelcome to this game, i think you know"
" the rules,so press enter to play:"
"remember, number only display %us!",delay);
scanf("%c",&play_again);
do
{
//initialize game
correct = true;
tries=0;
digits=2;
start_time=clock();
//inner loop for game
while(correct)
{
++tries;//a new attempt
wait_start=clock();
//generate a sequence of digits and display them
srand(time(&seed));
for(int i=1;i<=digits;++i)
printf("%d ",rand()%10);//output a random digit
// wait one second 从这开始一直到检查输入之前,我知道有问题但我找不出是什么问题...
for(;clock()-wait_start<delay*CLOCKS_PER_SEC;);
//overwrite the digit sequence
printf("\r");
for(int i=1;i<=digits;i++)
printf(" ");
if(tries==1)
printf("\nEnter the sequence\n");
else
printf("\r");
//input sequence and check it against the original
srand(seed);//reinitialize sequence
for(int i=1;i<=digits;i++)
{
scanf("%d",&number);
getchar();
if(number!=rand()%10)
{
correct = false;
break;
}
}
//on every third successful try, increase length
if(correct &&((tries%3)==0))
++digits;
printf("%s\n",correct?"Correct!":"Wrong!");
}
//output the score when game finished
score=10*(digits-((tries%3)==1));
total_digits=digits*(((tries%3)==0)?3:tries%3);
if(digits>2)
total_digits+=3*((digits-1)*(digits-2)/2-1);
game_time=(clock()-start_time)/CLOCKS_PER_SEC-tries*delay;
if(total_digits>game_time)
score+=10*(game_time-total_digits);
printf("\n\nGame time was %ds,"
" Your score is %d\n",game_time,score);
fflush(stdin);
//check if user required new game
printf("\nDo you want to play again?(y/n):");
scanf("%c",&play_again);//这个scanf会被跳过
}
while(tolower(play_again)=='y');
return 0;
}
#include<ctype.h>
#include<stdlib.h>
#include<time.h>
#include<stdbool.h>
int main(void)
{
const unsigned int delay=1;
char play_again='Y';
int number=0;//user's input;
int tries=0;//attempt
int digits=0;// number of digits in sequence
time_t seed=0;//seed value for random number sequence
time_t wait_start=0;// stores current time
bool correct=true;
int score=0;
int total_digits=0;
int game_time=0;
clock_t start_time=0;
printf("\nWelcome to this game, i think you know"
" the rules,so press enter to play:"
"remember, number only display %us!",delay);
scanf("%c",&play_again);
do
{
//initialize game
correct = true;
tries=0;
digits=2;
start_time=clock();
//inner loop for game
while(correct)
{
++tries;//a new attempt
wait_start=clock();
//generate a sequence of digits and display them
srand(time(&seed));
for(int i=1;i<=digits;++i)
printf("%d ",rand()%10);//output a random digit
// wait one second 从这开始一直到检查输入之前,我知道有问题但我找不出是什么问题...
for(;clock()-wait_start<delay*CLOCKS_PER_SEC;);
//overwrite the digit sequence
printf("\r");
for(int i=1;i<=digits;i++)
printf(" ");
if(tries==1)
printf("\nEnter the sequence\n");
else
printf("\r");
//input sequence and check it against the original
srand(seed);//reinitialize sequence
for(int i=1;i<=digits;i++)
{
scanf("%d",&number);
getchar();
if(number!=rand()%10)
{
correct = false;
break;
}
}
//on every third successful try, increase length
if(correct &&((tries%3)==0))
++digits;
printf("%s\n",correct?"Correct!":"Wrong!");
}
//output the score when game finished
score=10*(digits-((tries%3)==1));
total_digits=digits*(((tries%3)==0)?3:tries%3);
if(digits>2)
total_digits+=3*((digits-1)*(digits-2)/2-1);
game_time=(clock()-start_time)/CLOCKS_PER_SEC-tries*delay;
if(total_digits>game_time)
score+=10*(game_time-total_digits);
printf("\n\nGame time was %ds,"
" Your score is %d\n",game_time,score);
fflush(stdin);
//check if user required new game
printf("\nDo you want to play again?(y/n):");
scanf("%c",&play_again);//这个scanf会被跳过
}
while(tolower(play_again)=='y');
return 0;
}
[此贴子已经被作者于2020-4-2 17:59编辑过]