2016-04-30 14 views
0

不正な番号が選択された場合、プログラムを最初からループバックさせる方法を教えてください。私は何が間違っているのか分かりません。私はif秒、do while秒、while秒、およびif else Sを試してみました:乱数ジェネレータが失敗する

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ArrayProblms 
{ 
    class Program 
    { 
     public static void Main(string[] args) 
     { 
      Console.WriteLine("Guess a number between 1 and 10: "); 
      RandomNumberGenerator(); 
      Console.ReadLine(); 
     } 

     public static void RandomNumberGenerator() 
     { 
      Random rand = new Random(); 
      int userValue = int.Parse(Console.ReadLine()); 
      int randValue = rand.Next(1, 11); 
      int attempts = 0; 

      if (userValue == randValue) 
      { 
       Console.WriteLine("You have guessed correctly!"); 
      } 
      while (userValue != randValue) 
      { 
       Console.WriteLine("You have guessed incorrectly"); 
       attempts++; 
       Console.WriteLine("You have made {0} incorrect guesses", attempts); 
       break; 
      } 
     } 
    } 
} 
+0

プログラムと何、何度も何度も通過ループに条件ですが終了するための条件は何ですか? –

答えて

0

私は彼が正しく推測するまで、新しい番号を入力するようユーザーに求め続けることdo...whileを使用します。以下

例:

public static void RandomNumberGenerator() 
{ 
    Random rand = new Random(); 

    int randValue = rand.Next(1, 11); 
    int attempts = 0; 

    // do...while cycle to ask user to enter new value each time the used has been wrong 
    do 
    { 
     // read user input 
     int userValue = int.Parse(Console.ReadLine()); 

     // if user guessed correct 
     if (userValue == randValue) 
     { 
      Console.WriteLine("You have guessed correctly!"); 
      // go away from do...while loop 
      // it will stop asking user and will exit from the method 
      break; 
     } 

     // if user has been wrong 
     Console.WriteLine("You have guessed incorrectly"); 
     // increment attempts count 
     attempts++; 
     Console.WriteLine("You have made {0} incorrect guesses", attempts); 
    } 
    // and repeat until user guessed correctly 
    while(userValue != randValue) 
} 
+1

'これを試してください:'は何も説明しません。私たちはあなたが修正したものを見るためにコードを1行ずつ比較しなければなりませんか? – Eser

+0

サンプルコードの内容を説明するコメントを付けます。私はそれが助けてくれるといい – MaKCbIMKo

0

あなたは正しい軌道に乗っているが、あなたがた場合にのみループの外whileループとbreakConsole.ReadLineを配置する必要がありますユーザーの値が一致します。この擬似コードのような

何か:

Generate random number 
while (true) { 
    Get value from user 
    If it matches, break 
} 
1

あなたは、ループ内int userValue = int.Parse(Console.ReadLine());を入れて、反復ごとに入力をチェックする必要があります。 breakだけuserValue == randValue場合でなければなりません:

public static void RandomNumberGenerator() 
    { 
     Random rand = new Random(); 

     int randValue = rand.Next(1, 11); 
     int attempts = 0; 


     while (true) 
     { 
      int userValue = int.Parse(Console.ReadLine()); // input inside the loop 
      if (userValue == randValue) // checking inside the loop 
      { 
       Console.WriteLine("You have guessed correctly!"); 
       break; 
      } 

      Console.WriteLine("You have guessed incorrectly"); 
      attempts++; 
      Console.WriteLine("You have made {0} incorrect guesses",   attempts);     
     } 

    } 
関連する問題