2016-06-26 3 views
0

C.内のランダム数推測ゲーム最初のprintfステートメントの後にスペースを追加するにはどうしたらいいですか?

正しい数が推測されない場合、ゲームが終了する前に、ユーザーは10の推測を持っています。

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

int main() { 
int theNumber; 
int userGuess; 
int counter = 10; 
int lowerBound = 1, upperBound = 100; 

// seed function 
srand((unsigned)time(NULL)); 

// generates number between 1 and 100 
theNumber = lowerBound + rand() % (upperBound - lowerBound + 1); 

printf("Please guess a number (1-100): "); 

for (counter = 9; counter <= 10; counter--) { 

    // get users guess 
    scanf("%d", &userGuess); 

    if (userGuess == theNumber) { 
     printf("You guessed it! The number was: %d", theNumber); 
     break; 
    } 

    if (userGuess < theNumber && counter >= 1) 
     printf("Your guess was too low. You have %d guesses remaining. Guess higher: ", counter); 

    if (userGuess > theNumber && counter >= 1) 
     printf("Your guess was too high. You have %d guesses remaining. Guess lower: ", counter); 

    if (counter == 0) { 
     printf("\nYou ran out of guesses. The number was: %d \n", theNumber); 
     break; 
    } 

} 

return 0; 

} 

サンプル出力:出力は次のようになりますように

Please guess a number (1-100): 100 
Your guess was too high. You have 9 guesses remaining. Guess lower: 90 

どのように私は、最初のprintf文の後にスペースを追加することができますか?

所望の出力:

Please guess a number (1-100): 100 

Your guess was too high. You have 9 guesses remaining. Guess lower: 90 
+0

'\ N '?改行? – Li357

+0

私はそれを試しました。最初のprintf文の後に\ nを置くと、最初にユーザが推測するのは次の行に壊れてしまい、そのようなことは起こりたくありません。可能であれば、最初の行の右側にとどまりたいと思います。 – ZSC

+0

真剣に?なぜあなたは他に試しましたか?ヒント:**入力後に**印刷したい場合は、**入力前に**印刷することは論理的には機能しません。 – Olaf

答えて

2

!何が起こるのですか?
は常にエラーをチェック:

if (scanf("%d", &userGuess) != 1){ 
    printf("incorrect input.\n"); 
    return 1; 
} 

あなたが縦のスペース使用printf("\n");が必要な場合。
あなたは一度だけ使用し、縦のスペースが必要な場合:

if (counter == 9) 
    printf("\n"); 

サンプルコードを:

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

int main() { 
    int theNumber; 
    int userGuess; 
    int counter = 10; 
    int lowerBound = 1, upperBound = 100; 

    // seed function 
    srand((unsigned)time(NULL)); 

    // generates number between 1 and 100 
    theNumber = lowerBound + rand() % (upperBound - lowerBound + 1); 

    printf("Please guess a number (1-100): "); 

    for (counter = 9; counter <= 10; counter--) { 

     // get users guess 
     if (scanf("%d", &userGuess) != 1){ 
      printf("incorrect input.\n"); 
      return 1; 
     } 
     //if (counter == 9) 
     printf("\n"); 

     if (userGuess == theNumber) { 
      printf("You guessed it! The number was: %d", theNumber); 
      break; 
     } 

     if (userGuess < theNumber && counter >= 1) 
      printf("Your guess was too low. You have %d guesses remaining. Guess higher: ", counter); 

     if (userGuess > theNumber && counter >= 1) 
      printf("Your guess was too high. You have %d guesses remaining. Guess lower: ", counter); 

     if (counter == 0) { 
      printf("\nYou ran out of guesses. The number was: %d \n", theNumber); 
      break; 
     } 

    } 

    return 0; 
} 
0

あなたは単にあなたの最初のprintf文の後printf("\n");を追加することによってそれを行うことができ、これらの2行の間に新しい空白行について尋ねている場合。

どこにでもスペースを入れたい場合は、printf(" ");を使用してください。これにより、スペースが追加されます。

intとwhileループを使用して、空白または空白行を必要な数だけ印刷します。

例:ユーザーが '1' のようなテキストを入力した場合

int main() 
{ 
    int i; 
    i = 0; 
    while (i < 30) 
    { 
     printf(" "); 
     i++; 
    } 
    printf("X\n"); 
    return 0; 
} 
+0

'main()' - 'int main(void){...} 'を定義するための正しい方法の1つ – babon

+0

@babonありがとうございます。基本的には引数が渡​​されないようにしたい。私が初心者なので私が間違っている場合は、私を修正してください。 –

+1

'()' - 関数は不特定多数の引数をとります。 '(void)' - 関数は絶対に引数をとりません。このコードはhttp://hastebin.com/muyucoqafa.coffeeで遊んでも構いません。 – babon

関連する問題