2014-02-23 29 views
0

猜謎遊戲允許用戶有5次嘗試,如果他們沒有在5次嘗試中嘗試,那麼他們會失敗。但在第五次嘗試中,即使你的答案是正確的,該計劃仍然會說你輸了。請任何人都可以告訴我,我需要在我的代碼中修正,以便在第五次嘗試時猜對:它會說「你贏了」。謝謝。在C#中猜謎遊戲有5個嘗試

static void Main(string[] args) 
     { 
      //Create an integer variable to hold a redom number 
      int answer = 0; 
      int guess = 0; 

      //Creates an object of the Random class 
      Random number = new Random(); 

      answer = number.Next(1, 11); 
      //Creates for loop 
      for (int i=1; i<=5; i++) 
      { 

       Console.Write("Enter Guess {0}:", i); 

       guess = Convert.ToInt32(Console.ReadLine()); 
        if (i >= 5) 
        { 
         Console.WriteLine("Your Lose. The answer is {0}", answer); 
        } 
        else 
        { 
         if (guess==answer) 
         { 

          Console.WriteLine("You Won!! {0} is the correct number", answer); 
          break; 
         } 

         else if (guess < answer) 
         { 
          Console.WriteLine("Guess is higher"); 
         } 

         else if (guess > answer) 
         { 
          Console.WriteLine("Guess is lower"); 
         } 
        }//end if 
      }//end of for loop 

      //Pause Display 
      Console.ReadKey(); 
     }//end of Main** 
+0

'我f(i> 5)'將是一個開始的地方。現在你允許猜測1,2,3,4,並立即放棄猜測5.但是你的for()循環會在迭代#6 ANYWAYS中終止,你應該在循環外面放置「太多猜測」。 –

回答

0

你只需要把if聲明這些行前:

Console.Write("Enter Guess {0}:", i); 
guess = Convert.ToInt32(Console.ReadLine()); 

試試這個:

for (int i=0; i<=5; i++) 
{ 
    if (i == 5) 
    { 
     Console.WriteLine("Your Lose. The answer is {0}", answer); 
     break; 
    } 
    Console.Write("Enter Guess {0}:", i); 
    guess = Convert.ToInt32(Console.ReadLine()); 
    if (guess==answer) 
    { 
     Console.WriteLine("You Won!! {0} is the correct number", answer); 
     break; 
    } 
    else if (guess < answer) 
    { 
     Console.WriteLine("Guess is higher"); 
    } 
    else if (guess > answer) 
    { 
     Console.WriteLine("Guess is lower"); 
    } 
} 
+0

親愛的,沒有'i> 5'的機會,它可以是'i == 5' for循環IMO –

+0

,而不是將i設置爲6,如果成功,您可以從循環中斷開。 –

0

您需要創建一個變量,表示它的搜索是否成功。在循環結束時。

static void Main(string[] args) 
    { 
     //Create an integer variable to hold a redom number 
     int answer = 0; 
     int guess = 0; 
     bool searchStatus=fale; 
     //Creates an object of the Random class 
     Random number = new Random(); 

     answer = number.Next(1, 11); 
     //Creates for loop 
     for (int i=1; i<=5; i++) 
     { 

      Console.Write("Enter Guess {0}:", i); 

      guess = Convert.ToInt32(Console.ReadLine()); 


        if (guess==answer) 
        { 

         Console.WriteLine("You Won!! {0} is the correct number", answer); 
         searchStatus=true; 
         break; 
        } 

        else if (guess < answer) 
        { 
         Console.WriteLine("Guess is higher"); 
        } 

        else if (guess > answer) 
        { 
         Console.WriteLine("Guess is lower"); 
        } 

     }//end of for loop 

     if(!searchStatus) 
     { 
     Console.WriteLine("Your Lose. The answer is {0}", answer); 
     } 

     //Pause Display 
     Console.ReadKey(); 
    }//end of Main** 
0

你只需要調整Main方法裏面有點:

//Create an integer variable to hold a redom number 
int answer = 0; 
int guess = 0; 

//Creates an object of the Random class 
Random number = new Random(); 

answer = number.Next(1, 11); 
//Creates for loop 
for (int i = 1; i <= 6; i++) 
{ 

    if (i > 5) 
    { 
     Console.WriteLine("Your Lose. The answer is {0}", answer); 
     break; 
    } 

    Console.Write("Enter Guess {0}:", i); 
    guess = Convert.ToInt32(Console.ReadLine()); 

    if (guess == answer) 
    { 
     Console.WriteLine("You Won!! {0} is the correct number", answer); 
     break; 
    } 
    else if (guess < answer) 
    { 
     Console.WriteLine("Guess is lower"); 
    } 
    else if (guess > answer) 
    { 
     Console.WriteLine("Guess is higher"); 
    } 
}//end of for loop 

//Pause Display 
Console.ReadKey();