-2

所以对于这个任务,我必须包含一个再次播放的功能。这意味着一旦人们猜对了,程序应该让用户选择是否再次播放。另外,我正在尝试包含一个功能,如果用户在 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;
}
4

2 回答 2

1

一个简单的方法是创建一个 bool 变量(最初设置为 true),可以在 while 语句中检查该变量,并在用户获得继续或不继续选项后进行更新。然后只需将您的休息时间更改为继续,您应该处于良好状态。

于 2013-03-12T02:19:52.880 回答
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
于 2013-03-12T02:21:57.340 回答