-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 に答える