0

ユーザーがリストからラジオ局を選択するように求められるメニュー システムをセットアップしています。後で使いやすくするために、そのリストを StationList というファイルに入れたいと考えています。そのビットはすべてソートされていますが、選択プロセスに問題があります。

すべて手動で入力する代わりに、case ステートメントで有効なケースの StationList を参照する方法はありますか? 私は周りを見回しましたが、すぐに答えがあるようには見えません.2週間しか学習していないことを覚えておいてください:)

前もって感謝します!

例:

i = (an element from iterating through StationList)

switch (selection)
{
case (i):
    i = (int)Choice.GoodChoice;
    Console.WriteLine("You chose " + selection + " radio!");
    break;
case "!exit":
case "!!":
    i = (int)Choice.ExitChoice;
    break;
case "!info":
    TitleScreen();
    Console.ForegroundColor = ConsoleColor.Green;
    break;
default:
    Console.WriteLine("Invalid selection! Please try again!");
    break;
}
4

1 に答える 1

1

不可能だよ。

可能であれば、ケースを自動的に切り替えるスイッチcaseあると想像してください。各sで何を行うかをどのように定義しますか?

編集:
選択がリストの文字列の1つであるかどうかを確認できるようにする必要があります。したがって、基本的に (1) すべての文字列を に追加する必要がありますHashSet。(2) コードは次のようになります。

HashSet hashset = new HashSet();
using (var file = new StreamReader(path))
{
    string line;
    while ((line = file.ReadLine()) != null)
        hashset.Add(line);
}
// ...

if (hashset.Contains(selection))
{
    i = (int)Choice.GoodChoice;
    Console.WriteLine("You chose " + selection + " radio!");
}
else
{
    switch (selection)
    {
    case "!exit":
    case "!!":
        i = (int)Choice.ExitChoice;
        break;
    case "!info":
        TitleScreen();
        Console.ForegroundColor = ConsoleColor.Green;
        break;
    default:
        Console.WriteLine("Invalid selection! Please try again!");
        break;
    }
}
于 2010-11-10T00:32:39.840 に答える