2017-10-03 5 views
-1

私はC言語で「推測数」のゲームをプレイするプログラムを持っています。それは正しく実行されますが、ユーザが2つの数字(最初は1、もう一度は1)を入力すると、試行回数が限られているとプログラムが繰り返されます。ここで「推測数」Cしかし、文が正しく機能しない場合

は、プログラムのために私のコードです:

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

int main(void) 
{ 
    //----Guess a Number Game.----------------------------------------- 
    // srand and rand needs the #include <stdlib.h> library 

    srand(time(NULL)); //seeding the guess a number game with the system time, so the guess a # game always starts at a different point. 
    int guess; 
    int correctnum; 
    correctnum = rand(); 

    printf("Enter a number:"); 
    scanf("%i",&guess); 

    if(guess>correctnum) // If aa is greater than bb AND aa is greater than cc. 
    { 
     printf("Please enter another number, lower this time!"); 
     scanf("%i",&guess); 
     main(); 
    } 

    else if (guess<correctnum) 
    { 
     printf("Please enter another number, higher this time!"); 
     scanf("%i",&guess); 
     main(); 
    } 

    else if (guess==correctnum) 
    { 
     printf("You are a WINNER!\n"); 
     printf("You guessed the number right and it was %i!\n",correctnum); 
    } 
    int repeat; 
    printf("Would you like to play again? 1=Yes and 2=No."); 
    scanf("%i",&repeat); 

    if(repeat==1) 
    { 
    main(); 
    } 
    if(repeat==2) 
    { 
    printf("Hope you had a good time playing the game! See you soon!\n"); 
    return 0; 
    } 
} 
+6

は*** ***ループを使用して、再帰的に 'main'を呼び出さないでください。 –

+0

あなたの質問については、実行する必要がある「反復回数」に制限はありません。 –

+0

'main()'を呼び出す前に 'scanf()'を使い、関数の先頭でそれを再利用します。 – YaatSuka

答えて

1

は、再帰的にmain()を呼び出さないでください。必要なものは単純なループです。おそらく、()あまりにもscanf関数からエラーをチェックする必要があり

int main(void) { 
    srand(time(NULL)); 
    int correctnum = rand(); 
    int guess; 
    int done = 0; 

    while (! done) { 
     printf("Enter a number:"); 
     scanf("%i",&guess); 

     if (guess>correctnum) { 
      printf("Please enter another number, lower this time!"); 
     } else if (guess<correctnum) { 
      printf("Please enter another number, higher this time!"); 
     } else /* if (guess==correctnum) */ { 
      printf("You are a WINNER!\n"); 
      printf("You guessed the number right and it was %i!\n",correctnum); 

      done = 1; 
     } 
    } 
} 

が、最初の最初のもの:のようなもの。

+0

あなたの例では、ループごとに 'scanf()'を2回使用します。 – YaatSuka

+0

'main(){...}'は1999年から有効ではありません。 – melpomene

0

あなたがループ内で必要なすべての機能を実装できます。

#include <stdio.h> 
#include <time.h> 
#include <stdlib.h> 

void main(){ 
    srand(time(NULL)); 
    int guess; 
    int correctnum; 
    correctnum = rand() % 100; #it's better to get a number between 1~100 
    int done = 1; 

    printf("guess the number:\t"); 
    scanf("%i", &guess); 

    while(done){ 
     if(guess < correctnum){ 
      printf("Please enter another number, higher this time:\t"); 
      scanf("%i", &guess);  
     } 

     else if(guess > correctnum){ 
      printf("Please enter another number, lower this time:\t"); 
      scanf("%i", &guess); 
     } 

     else if(guess == correctnum){ 
      printf("well done! if you want to play again, input 1, else input 0:\t"); 
      scanf("%i", &done); 
      if(done == 1){ 
       correctnum = rand() % 100; 
       printf("guess the number:\t"); 
       scanf("%i", &guess); 
      } 
     } 
    } 

    printf("Hope you had a good time playing the game! See you soon!\n"); 
} 
関連する問題