2017-09-03 3 views
2

ここに学生がいます。ユーザー入力(C#)に基づいて配列の中で最高の値を探しています

現在、ユーザーの入力に基づいて配列の中で最高の値を見つけるためにプロジェクトに取り組んでいます。

私が使用している現在のforeachループは、ユーザーの入力を受け取り、2番目の配列で一致する最初のインスタンスのみを検索します。

私は2つの方法を試しました。どちらも同じ結果に終わります。

まず、リストを作成してソートして元に戻そうとしました。そのように私は0のインデックスを取ることができ、それが最高

static void Main(string[] args) 
    { 
     string[] fishColors = new string[15] { "pink", "purple", "red", "orange", "blue", "green", "pink", "green", "blue", "red", "orange", "purple", "green", "red", "purple" }; 
     int[] fishLengths = new int[15] { 49, 5, 45, 10, 14, 1, 44, 17, 48, 11, 13, 17, 20, 15, 37 }; 

     List<int> userFishLengths = new List<int>(); 

     int userChoice = 0; 
     string input = null; 
     int longestFish = 0; 

     do { 
      Console.WriteLine("Please select the number from the list below for the color of fish you would like to choose:\r\n0. Pink\r\n1. Purple\r\n2. Red\r\n3. Orange\r\n4. Blue\r\n5. Green"); 
      input = Console.ReadLine(); 
     } while (Int32.TryParse(input, out userChoice) == false) ; 

     string userColor = fishColors[userChoice]; 

     foreach (string fish in fishColors) 
     { 
      if (userColor == fish) 
      { 
       int indexID = Array.IndexOf(fishColors, fish); 
       int fishLength = fishLengths[indexID]; 
       userFishLengths.Add(fishLength); 
      } 
     } 

     userFishLengths.Sort(); 
     userFishLengths.Reverse(); 

     Console.WriteLine("The longest fish in the tank with the color you chose (" + userColor + ") is " + userFishLengths[0]+" inches."); 

    } 

は第二になり、私は毎回、それを取り、それは大きなだ場合は、変数を上書き値を作成しようとしました。

static void Main(string[] args) 
    { 
     string[] fishColors = new string[15] { "pink", "purple", "red", "orange", "blue", "green", "pink", "green", "blue", "red", "orange", "purple", "green", "red", "purple" }; 
     int[] fishLengths = new int[15] { 49, 5, 45, 10, 14, 1, 44, 17, 48, 11, 13, 17, 20, 15, 37 }; 

     int userChoice = 0; 
     string input = null; 
     int longestFish = 0; 

     do { 
      Console.WriteLine("Please select the number from the list below for the color of fish you would like to choose:\r\n0. Pink\r\n1. Purple\r\n2. Red\r\n3. Orange\r\n4. Blue\r\n5. Green"); 
      input = Console.ReadLine(); 
     } while (Int32.TryParse(input, out userChoice) == false) ; 

     string userColor = fishColors[userChoice]; 

     foreach (string fish in fishColors) 
     { 
      if (userColor == fish) 
      { 
       int indexID = Array.IndexOf(fishColors, fish); 
       int fishLength = fishLengths[indexID]; 

       if (fishLength > longestFish) 
       { 
        longestFish = fishLength; 
       } 
      } 
     } 

     Console.WriteLine("The longest fish in the tank with the color you chose (" + userColor + ") is " + longestFish + " inches."); 

    } 

ご協力いただきありがとうございます。ありがとうございました!

+0

のために設計されているユーザーが色を選択することになっている、とプログラムが長さで応答していることでしょうか? –

+0

修正プログラムは、ユーザーが選択した色の最大長で応答します。 –

+0

ああ、私はダニエルメイズの答えに行くだろう。色を一意の識別子にする必要がある場合は、それらをキーとしたディクショナリと値としての対応する長さ配列を使用するのが最適な方法です。 –

答えて

1

問題はArray.IndexOf()コールにあります。あなたのfishColors配列の

int indexID = Array.IndexOf(fishColors, fish); 

内容は一意ではありませんので、Array.IndexOf(fishColors, fish)コールは、単に最初一致した要素のインデックスを返しています。 (例えば"pink" = 0"red" = 2


これらの値を格納するために異なるデータ構造を使用して、適しであろう。例えば、Dictionary<TKey,TValue>を使って調べてください。

var fish = new Dictionary<string, int[]>() 
{ 
    { "pink", new[] { 49, 44 } }, 
    { "purple", new[] { 5, 17, 37 } } 
}; 

これにより、色に関連する長さを簡単に調べることができます。あなたは、両方のアレイの使用を保持しなければならない場合


あるいは、あなたの代わりにforeachの簡単なforループでこれを行うことができます。あなたが使用しているため

for (int i = 0; i < fishColors.Length; i++) 
{ 
    if (userColor == fishColors[i]) 
    { 
     int fishLength = fishLengths[i]; 

     if (fishLength > longestFish) 
     { 
      longestFish = fishLength; 
     } 
    } 
} 
+0

ご連絡ありがとうございます。 おそらく最も簡単な方法であり、最も理にかなっています。しかし、学校のプロジェクトのために、私は2つの配列を使用する必要があります。 (申し訳ありませんが、あまりにも速く入力してください) –

+0

@DylanColeDuke確かに、これらの制約で二次的な答えを書きましょう。 (編集 - 追加をチェックしてください) –

+0

はい、その要件を満たしています。それは完璧です、あなたの助けに感謝! –

0

スクリプトは、動作しませんでした。いつもあなたの最初の試合ではなく、現在のペアを与える

int indexID = Array.IndexOf(fishColors, fish); 

を。

例: 「紫色」を検索して、常にエントリ5を取得しています。

私はあなたが多くをコーディング変更したくなかった場合、これはバージョン変更されます:

 static void Main(string[] args) 
     { 
      string[] fishColors = new string[15] { "pink", "purple", "red", "orange", "blue", "green", "pink", "green", "blue", "red", "orange", "purple", "green", "red", "purple" }; 
      int[] fishLengths = new int[15] { 49, 5, 45, 10, 14, 1, 44, 17, 48, 11, 13, 17, 20, 15, 37 }; 

      int userChoice = 0; 
      string input = null; 
      int longestFish = 0; 

      do 
      { 
       Console.WriteLine("Please select the number from the list below for the color of fish you would like to choose:\r\n0. Pink\r\n1. Purple\r\n2. Red\r\n3. Orange\r\n4. Blue\r\n5. Green"); 
       input = Console.ReadLine(); 
      } while (Int32.TryParse(input, out userChoice) == false); 

      string userColor = fishColors[userChoice]; 

      int indexID = 0; 
      foreach (string fish in fishColors) 
      { 
       if (userColor == fish) 
       { 
        int fishLength = fishLengths[indexID]; 

        if (fishLength > longestFish) 
        { 
         longestFish = fishLength; 
        } 
       } 
       indexID++; 
      } 

      Console.WriteLine("The longest fish in the tank with the color you chose (" + userColor + ") is " + longestFish + " inches."); 
      Console.ReadKey(); 
     } 

をしかしLINQを、あなたはそれをよりシンプルにすることができます:

 static void Main(string[] args) 
     { 
      string[] fishColors = new string[15] { "pink", "purple", "red", "orange", "blue", "green", "pink", "green", "blue", "red", "orange", "purple", "green", "red", "purple" }; 
      int[] fishLengths = new int[15] { 49, 5, 45, 10, 14, 1, 44, 17, 48, 11, 13, 17, 20, 15, 37 }; 

      int userChoice = 0; 
      string input = null; 
      int longestFish = 0; 

      do 
      { 
       Console.WriteLine("Please select the number from the list below for the color of fish you would like to choose:\r\n0. Pink\r\n1. Purple\r\n2. Red\r\n3. Orange\r\n4. Blue\r\n5. Green"); 
       input = Console.ReadLine(); 
      } while (Int32.TryParse(input, out userChoice) == false); 

      string userColor = fishColors[userChoice]; 
      longestFish = fishColors 
       .Zip(fishLengths, (color, length) => new { color, length }) 
       .Where(s => s.color.Equals(userColor)).Max(x => x.length); 

      Console.WriteLine("The longest fish in the tank with the color you chose (" + userColor + ") is " + longestFish + " inches."); 
      Console.ReadKey(); 
     } 
+0

これは完璧です!ありがとうございました。 –

0

私が知っています、それは学生プロジェクトだということ。しかし、問題は非常に1 LINQのが、問題は何

string[] fishColors = new string[] //DONE: you have no need in specifing magic number "15" 
    { "pink", "purple", "red", "orange", "blue", "green", "pink", "green", "blue", 
     "red", "orange", "purple", "green", "red", "purple" }; 

    int[] fishLengths = new int[] //DONE: you have no need in specifing magic number "15" 
    { 49, 5, 45, 10, 14, 1, 44, 17, 48, 11, 13, 17, 20, 15, 37 }; 

    // Color/its index correspondence: 
    // Key - index: 1, 2, 3, ... 
    // Value - color: pink, purple, red, ... 
    var colors = fishColors 
    .Distinct() 
    .Select((color, index) => new { 
     color = color, 
     index = index + 1, }) 
    .ToDictionary(item => item.index, item => item.color); 

    string userColor = null; 

    while (true) { 
    Console.WriteLine("Please select the number from the list below for the color of fish you would like to choose:"); 

    //DONE: instead of hardcoding, build the string 
    Console.WriteLine(string.Join(Environment.NewLine, colors 
     .OrderBy(pair => pair.Key) 
     .Select(pair => $"{pair.Key}. {pair.Value}"))); 

    //DONE: input is valid if and only iff it's integer and it corresponds to color 
    if (int.TryParse(Console.ReadLine(), out var code) && // <- out var - C# 7.0 Syntax 
     colors.TryGetValue(code, out userColor)) 
     break; 
    } 

    //DONE: zip colors and lengths, filter out userColor fish only, get maximum 
    var result = fishColors 
    .Zip(fishLengths, (color, length) => new { color = color, length = length }) 
    .Where(item => item.color == userColor) 
    .Max(item => item.length); 

    //DONE: do not concat string, but use string interpolation (or formatting) 
    Console.WriteLine($"The longest fish in the tank with the color you chose ({userColor}) is {result} inches."); 
関連する問題