用C语言编写一个SIMON小游戏。 50
#include<stdio.h>#include<ctype.h>#include<stdbool.h>#include<stdlib.h>#include<time....
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
char another_game = 'Y';
int correct = false;
int counter = 0;
int sequence_length = 0;
time_t seed = 0;
int number = 0;
time_t now = 0;
int 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("\n 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(time(&seed));
for(int i = 1; i <= sequence_length; i++);
printf("%d",rand()%10); //输出一个随机数字。
for(; clock() - now < 1*CLOCKS_PER_SEC;);
printf("\r");
for(int 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(int 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);
printf("\n\n Your scors is %d",--counter *100/time_taken); //输出比赛得分。
fflush(stdin);
printf("\n Do you wan to play again (y/n)?");
scanf("%c",&another_game);
}
while(toupper(another_game) == 'Y');
return 0;
}
问题出在编译运行后只显示一个数,而要求是输入两个数,另一个数,一直看不到显示。用了网上另一段代码又确定可以运行。。求解。 展开
#include<ctype.h>
#include<stdbool.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
char another_game = 'Y';
int correct = false;
int counter = 0;
int sequence_length = 0;
time_t seed = 0;
int number = 0;
time_t now = 0;
int 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("\n 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(time(&seed));
for(int i = 1; i <= sequence_length; i++);
printf("%d",rand()%10); //输出一个随机数字。
for(; clock() - now < 1*CLOCKS_PER_SEC;);
printf("\r");
for(int 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(int 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);
printf("\n\n Your scors is %d",--counter *100/time_taken); //输出比赛得分。
fflush(stdin);
printf("\n Do you wan to play again (y/n)?");
scanf("%c",&another_game);
}
while(toupper(another_game) == 'Y');
return 0;
}
问题出在编译运行后只显示一个数,而要求是输入两个数,另一个数,一直看不到显示。用了网上另一段代码又确定可以运行。。求解。 展开
1个回答
展开全部
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
char another_game = 'Y';
int correct = false;
int counter = 0;
int sequence_length = 0;
time_t seed = 0;
int number = 0;
time_t now = 0;
int 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("\n When you do, you must put spaces between the digits.\n");
printf("\nGood Luck!\nPress Enter to play!\n");
getch(); /*这里用getch就可以了,敲任意键不用回车*/
do
{
correct = true; //在默认的情况下,显示正确的输入。
counter = 0 ; //初始化成功,并开始计数。
sequence_length = 2; //最初的游戏序列长度。
time_taken = clock(); //在游戏开始时记录当前时间。
while(correct)
{
int i;
sequence_length += counter++ % 3 == 0;
seed = time(NULL);
now = clock();
srand(time(&seed));
for(i = 1; i <= sequence_length; i++) /*for后面的分号去掉,否则下面的printf不是循环体*/
printf("%d ",rand()%10); //输出一个随机数字。
for(; clock() - now < 1*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);
printf("\n\n Your scors is %d",--counter *100/time_taken); //输出比赛得分。
fflush(stdin);
printf("\n Do you wan to play again (y/n)?");
scanf("%c",&another_game);
}
while(toupper(another_game) == 'Y');
return 0;
}
看注释。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询