2
私はC#でコードを作成しています。そこで、作成するセットの数をユーザーに尋ね、そのセットに要素を入力します。それらのセットから、彼は2セットを選択し、選択されたセットの和集合を表示する。以下のコードで C#で2組のUnionを表示
は、セット内の要素は_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);
}
}
}
しかし、私はまだ_itemsのエラーを取得しています[A]の範囲の例外のうち、その引数は、未処理のだった...。このため –
理由は、世界中どこででも無効なインデックスをチェックしないことです。編集を参照してください。 –