2016-10-12 6 views
1

私は、推測したいと思う最大数をユーザーが設定する乱数推測ゲームを作成しています。私はコードの大部分を理解している、私が得ているように見える問題は、最大数を設定し、答えが常に0であることを推測し始めたことです。答えが数字であるように自分のコードを調整する方法に関するヒント範囲とは0ではない?乱数推測ゲームアンサー0

class Program 
{ 

    public static int SelectedNumber = 0; 
    public static Random ran = new Random(); 
    public static bool GameOver = false; 
    public static int UserMaxValue = 0; 
    public static string decision; 

    static void Main(string[] args) 
    { 

     int UserNumber; 
     SelectedNumber = ran.Next(0, UserMaxValue); 

     do 
     { 
      Console.WriteLine("What is the maximum number you want to guess from?"); 
      UserMaxValue = Convert.ToInt32(Console.ReadLine()); 

      do 
      { 
       Console.WriteLine("Select a number between 1 and {0}!", UserMaxValue); 
       UserNumber = Convert.ToInt32(Console.ReadLine()); 
       GuessNumber(UserNumber); 
      } while (GameOver == false); 
     } while (GameOver == false); 
    } 

    public static void GuessNumber(int UserNumber) 
    { 

     if (UserNumber < SelectedNumber) 
      Console.WriteLine("Your number is wrong, please try again!"); 
     else if (UserNumber > SelectedNumber) 
      Console.WriteLine("Your Number is wrong, please try again!"); 
     //else if (UserNumber == 0) 
     // Console.WriteLine("Your Number is wrong, please try again!"); 
     else 
     { 
      Console.WriteLine("Yay! You got the right number. Do you want to play again? (y/n) ", decision); 
      decision = Console.ReadLine(); 

      if (decision == "n") 
       GameOver = true; 
      else 
       do 
       { 
        Console.WriteLine("What is the maximum number you want to guess from?"); 
        UserMaxValue = Convert.ToInt32(Console.ReadLine()); 

        do 
        { 
         Console.WriteLine("Select a number between 1 and {0}!", UserMaxValue); 
         UserNumber = Convert.ToInt32(Console.ReadLine()); 
         GuessNumber(UserNumber); 
        } while (GameOver == false); 
       } while (GameOver == false); 
      } 
     } 
    } 
} 

答えて

2

あなたは、次の行を持っている。このため(私は間に休憩を省略しています)に:

public static int UserMaxValue = 0; 
// ... 
SelectedNumber = ran.Next(0, UserMaxValue); 
// ... 
Console.WriteLine("What is the maximum number you want to guess from?"); 
     UserMaxValue = Convert.ToInt32(Console.ReadLine()); 

あなたは正しくSelectedNumberを設定することができます前に、UserMaxValueをユーザーに依頼する必要があり。

+0

さらに、ran.Nextは一度しか呼び出されません。ユーザーが再度再生を依頼するたびに呼び出されるはずです。 – Martheen

1

コードの修正が必要でした。変更のコメントを追加しました。 ユーザー入力にエラー処理を追加する方がよい。

UPDATE:無効な入力に対してエラー処理を追加しました。

class Program 
{ 
    public static int SelectedNumber = 0; 
    public static Random ran = new Random(); 
    public static bool GameOver = false; 
    public static int UserMaxValue = 0;   

    static void Main(string[] args) 
    { 
     int UserNumber = 0; 
     bool playAgain = false; 
     do 
     { 
      bool isUserMaxValueValid = false; 
      do 
      { 
       Console.WriteLine("What is the maximum number you want to guess from?"); 
       isUserMaxValueValid = int.TryParse(Console.ReadLine(), out UserMaxValue); 
      } while (!isUserMaxValueValid); 

      SelectedNumber = ran.Next(1, UserMaxValue); // Re-assign SelectedNumber for every new max number input. Random number start changed to 1 to avoid 0 value for SelectedNumber 

      do 
      { 
       bool isUserNumberValid = false; 
       do 
       { 
        Console.WriteLine("Select a number between 1 and {0}!", UserMaxValue); 
        isUserNumberValid = int.TryParse(Console.ReadLine(), out UserNumber); 
       } while (!isUserNumberValid); 

       playAgain = GuessNumber(UserNumber); 
       // I changed GameOver to see if the guessing portion is finished or not 
      } while (!GameOver); // You don't need to use GameOver == false 
     } while (playAgain); // Check if user wants to play again or not 
    } 


    public static bool GuessNumber(int UserNumber) 
    { 
     if (UserNumber != SelectedNumber) 
     { 
      GameOver = false; 
      Console.WriteLine("Your number is wrong, please try again!"); 
     } 
     else 
     { 
      GameOver = true; 
      bool isPlayAgainValid = false; 
      Console.WriteLine("Yay! You got the right number. Do you want to play again? (y/n)"); 
      do 
      { 
       string decision = Console.ReadLine(); 

       if (decision.Equals("n", StringComparison.InvariantCultureIgnoreCase)) 
       { 
        return false; 
       } 
       else if (decision.Equals("y", StringComparison.InvariantCultureIgnoreCase)) 
       { 
        break; 
       } 

       if(!isPlayAgainValid) 
       { 
        Console.WriteLine("Please enter y or n only."); 
       } 
      } while (!isPlayAgainValid);    


      // I removed redundant code 
     } 

     return true; 
    } 
} 
+0

大きい番号と小さい番号のチェックが同じメッセージを表示すると、重複しているように見えます(「番号」の場合は意図的ではありません)。バイナリ検索を使ってユーザを誘導するための明示的に異なるメッセージを与えるか、!= conditionに参加させるかのどちらかです。 また、ConsoleWriteLineの2番目のパラメータとしてdecisionを使用しても、文字列にコンポジットフォーマットがないため、何もしません。 – Martheen

+0

GuessNumberを使用してGameOver値を返し、PlayAgain DecisionをGameOverループの外側に配置することをお勧めします。 PlayAgainの 'return decision!=" n "'を短くすることもできます。 – kurakura88

+0

私はまた、TryParseをコードのどこかで使用して、ユーザが数字以外の何かを入力したというエラーメッセージを出す必要があると思います。私はそれがどこでどのように書かれなければならないのか分かりません。 –