2016-11-10 43 views
2

我在C#中有一個代碼,我要求用戶輸入他想創建的集合的數量,然後在這些集合中輸入元素。從這些集合中,他選擇2個集合並顯示所選集合的並集。在C#中的顯示聯盟2套#

在下面的代碼中,集合中的元素沒有被添加到_items,並且不顯示聯合。

感謝您的任何幫助。

namespace Union 
{ 

    class Program 
    { 
    static List<SortedSet<string>> _items = new List<SortedSet<string>>(); 
    static SortedSet<string> set = new SortedSet<string>(); 

    static void Main(string[] args) 
    { 
     int i, j, a, b; 
     string k; 
     Console.WriteLine("\n Enter the number of set to be used: "); 
     i = Convert.ToInt32(Console.ReadLine()); 
     for (j = 1; j <= i; j++) 
     { 
      SortedSet<string> set = new SortedSet<string>(); 
      do 
      { 
       Console.WriteLine("Enter first element in set {0}:", j); 
       k = Console.ReadLine(); 
       if (k != "stop") 
        set.Add(k); 
      } while (k != "stop"); 
      _items.Add(set); 
     } 
     Console.WriteLine("Enter index of 1st set of union:{0}"); 
     b = Convert.ToInt32(Console.ReadLine()); 
     Console.WriteLine("Enter index of 2nd set of union:{0}"); 
     c = Convert.ToInt32(Console.ReadLine()); 
     DisplayUnion(a, b); 
    } 

    public static void DisplayUnion(int a, int b) 
    { 
     SortedSet<string> set1 = _items[a]; 
     SortedSet<string> set2 = _items[b]; 
     set1.UnionWith(set2); 
     Console.WriteLine(set1); 
    } 
    } 
} 

回答

1

通過modifiing Main()DisplayUnion(int a, int b)方法,以達到更好的代表性,包括邊界案例場景完全編輯我的答案。 Main()方法:

static void Main(string[] args) 
{ 
    int i, j, a, b; 
    string k; 
    Console.WriteLine("Enter the number of sets to be used: "); 
    i = Convert.ToInt32(Console.ReadLine()); 
    for (j = 1; j <= i; j++) 
    { 
     SortedSet<string> set = new SortedSet<string>(); 
     var index = 0; 
     do 
     { 
      index++; 
      Console.WriteLine($"Enter {index} element in set {j}:"); 
      k = Console.ReadLine(); 
      if (k != "stop") 
       set.Add(k); 
     } while (k != "stop"); 
     _items.Add(set); 
    } 

    if (_items.Count == 0) 
    { 
     Console.WriteLine("You have no sets to union."); 
     return; 
    } 

    if (_items.Count == 1) 
    { 
     Console.WriteLine("Union of only set is: " + string.Join("", _items[0])); 

     return; 
    } 

    while (true) 
    { 
     Console.WriteLine("Enter index of 1st set of union:{0}"); 
     a = Convert.ToInt32(Console.ReadLine()); 
     if (a < _items.Count) 
     { 
      break; 
     } 

     Console.WriteLine($"Set {a} does not exists."); 
    } 

    while (true) 
    { 
     Console.WriteLine("Enter index of 2nd set of union:{0}"); 
     b = Convert.ToInt32(Console.ReadLine()); 
     if (b < _items.Count) 
     { 
      break; 
     } 

     Console.WriteLine($"Set {b} does not exists."); 
    } 

}

DisplayUnion(int a, int b)方法:

public static void DisplayUnion(int a, int b) 
{ 

    SortedSet<string> set1 = _items[a]; 
    SortedSet<string> set2 = _items[b]; 
    set1.UnionWith(set2); 
    Console.WriteLine($"Union of set {a + 1} with set {b + 1} is: " + string.Join("", set1)); 
} 

你,因爲你輸入一組索引無效,不檢查任何地方,它實際上invalid.I解決,通過增加接收outOfRangeException加兩個while cycles。 另外,我增加了兩個if statements邊界案件時,設置爲0或1.

希望它有幫助。

+0

但是我仍然在_items [a]中發現一個錯誤,那個參數超出範圍的異常未處理... –

+0

原因是您不檢查任何地方的無效索引。查看編輯。 –