したがって、この割り当てには、再生機能を含める必要があります。つまり、人が正しく推測したら、プログラムはユーザーにもう一度プレイするかどうかを選択できるようにする必要があります。また、ユーザーが 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;
}