2017-09-03 64 views
2

這裏的學生。根據用戶輸入尋找數組中的最大值(C#)

目前正在一個項目中尋找基於用戶輸入的數組中的最高值。

我正在使用的當前foreach循環獲取用戶輸入,然後只找到匹配第二個數組的第一個實例,而不是繼續循環。

我試過兩種方法。兩者都以相同的結果結束。

首先,我嘗試創建一個列表,然後排序和反轉。這樣我可以拿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

好的,我會和Daniel Mays一起回答。如果你需要顏色作爲一個唯一的標識符,那麼將它們作爲關鍵字的字典和它們對應的長度數組作爲值將是最好的方法。 –

回答

1

問題在於你的Array.IndexOf()調用。

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

fishColors數組的內容不是唯一的,並且因此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 } } 
}; 

這會讓你更容易查找與顏色相關的長度。


或者,如果你一定要保留使用兩個數組,你可以用一個簡單的for循環,而不是一個foreach做到這一點。

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

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

謝謝你的快速和明確的迴應。 我同意,這可能是最簡單的方法,並且最有意義。然而爲了學校的項目,我必須使用兩個數組。 (抱歉,我輸入太快) –

+0

@DylanColeDuke好的,讓我寫出你的第二個答案與這些約束。 (編輯 - 檢查添加內容。) –

+0

是的,可以滿足這些要求。這是完美的,謝謝你的幫助! –

0

腳本沒有工作,因爲你使用:

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

總是給你的第一場比賽,而不是目前的對。

例如: 您正在搜索「purple」,並始終輸入條目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

我知道,這是學生項目;但問題是非常一個的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."); 
相關問題