因此,对于这项任务,我必须再次包含一个游戏功能。意思是一旦人正确猜到该程序应该让用户选择是否再次玩。另外,我试图包含一个函数,如果用户在5次或更少的猜测中正确猜测,那么程序应该打印“Good Job!”如果它需要超过5个猜测,它应该显示“你可以做得更好!”。请帮帮我!我是编程的初学者,我一直试图解决这个问题。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main( void )
{
int i, n = 0, r;
int answer;
srand( time( NULL ) );
r = rand() %100 +1;
char userName[15];
printf("Welcome to GUESS MY NUMBER!\n\nPlease type your name here: ");
scanf("%s", &userName);
printf("\n\nI am thinking of a number between 1 and 100.\n\nCan you guess what it is? ");
while(scanf("%d", &i))
{
if (n >= 9 && i != r)
{
printf("\n\nSorry, the number was %d.\n", r);
printf("You should have gotten it by now.\n");
printf("Better luck next time.\n\n");
system ("PAUSE");
break;
}
if (i > r)
{
n++;
printf("Your guess is high. You only get 10 guesses. Try again: ");
}
else if (i < r)
{
n++;
printf("Your guess is low. You only get 10 guesses. Try again: ");
}
else if (i == r)
{
printf("\n\nCongratulations %s!\nYou guessed the number within %d guesses!\nWould you like to play again? y/n?\n",userName, n+1,answer);
scanf("%d", &answer);
system ("PAUSE");
break;
}
}
return 0;
}
答案 0 :(得分:1)
一件简单的事情是创建一个bool变量(最初设置为true),可以在while语句中检查并在用户被赋予继续或不继续的选项后更新。然后将你的休息时间改为继续,你应该保持良好状态。
答案 1 :(得分:0)
将整个事物包裹在另一个循环中,并在此外循环结束时,询问用户是否要再次播放。 while()或do-while()循环。如果用户说是,则继续循环,否则退出循环。
-Initialize the game
-Load any resources needed (in this case, none)
Begin looping continually
- Handle input
- Think
- Show results
End looping if exited
-Free any resources (in this case, none)
-Exit