有个问题,自己琢磨一天了,实在想不出,还得麻烦论坛里的朋友啊!!!
#include <stdio.h>#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
char another_game = 'Y';
bool correct = false;
int counter = 0;
int sequence_length = 0;
int i = 0;
time_t seed = 0;
int number = 0;
time_t now = 0;
time_t time_taken = 0;
printf("\nTo play Simple Simon,");
printf("watch the screen for a sequence of digits.");
printf("\nWatch carefully,as the digits are only displayed"
" for a second! ");
printf("\nThe computer will remove them, and then prompt you");
printf("to enter the same sequence.");
printf("\When you do, you must put spaces between the digits.\n");
printf("\nGood Luck!\nPress Enter to play\n");
scanf("%c",&another_game);
do
{
correct = true;
counter = 0;
sequence_length = 2;
time_taken = clock();
while(correct)
{
sequence_length += counter++%3 == 0;
seed = time(NULL);
now = clock();
srand((unsigned int)seed);
for(i = 1;i<=sequence_length;i++)
printf("%d",rand()%10);
for(;clock()-now<CLOCKS_PER_SEC;);
printf("\r");
for(i =1;i<=sequence_length;i++)
printf(" ");
if(counter==1)
printf("\nNow you enter the sequence_don't forget"
" the spaces\n");
else
printf("\r");
srand((unsigned int)seed);
for(i = 1;i<=sequence_length;i++)
{
scanf("%d",&number);
if(number !=rand()%10)
{
correct = false;
break;
}
}
printf("%s\n",correct?"correct!":"wrong!");
}
time_taken = (clock()-time_taken)/ CLOCKS_PER_SEC;
printf("\n\nYour score is %d",--counter * 100/time_taken);
fflush(stdin);
printf("\nDo you want to play again(y/n);");
scanf("%c",&another_game);
}
while(toupper(another_game)=='Y');
getchar();
getchar();
return 0;
}
执行后,我正确的输入结果也是“wrong”,我怀疑下面的语句有问题:
for(i = 1;i<=sequence_length;i++)
{
scanf("%d",&number);
if(number !=rand()%10)
{
correct = false;
break;
}
}
我认为scanf读取的是所有的数字,而if(number !=rand()%10)的意思是是对每个数字进行判断,所以问题应该出在这里,而我又不知道怎么改,恳请哪位老师帮忙看一下,我万分感激!!,语句很长,您幸苦了!!!!!