簡體   English   中英

如何循環 C# 中的 switch 語句

[英]How do I loop round a switch statement in C#

 class Program
 {
    static void Main(string[] args)
    {
        string Studentname;
        string retry = "No";

        Console.WriteLine("What is the Student's name? ");

        while (retry != "No")
             Console.WriteLine("What is the Student's name? ");

        Studentname = Console.ReadLine();

        switch (Studentname) {
            case "George":
                Console.WriteLine("Yes in the list");
                Console.ReadLine();
                break;

            case "Goblin":
                Console.WriteLine("Yes in the list");
                Console.ReadLine();
                break;

            case "Peter":
                Console.WriteLine("Yes in the list");
                Console.ReadLine();
                break;

            case "TJ":
                Console.WriteLine("Yes in the list");
                Console.ReadLine();
                break;

            default:
                Console.WriteLine("Not in the list");
                Console.WriteLine("Would you like to retry?");
                retry = Console.ReadLine();
                break;
        }
    }
}

如果答案不正確,我會嘗試循環 case 語句,但是語句總是中斷並且我無法循環。 我將如何解決這個問題?

關鍵字“ break”僅跳出它所在的第一個代碼塊({...}之間的空格)。開關中的break根本不應該干擾while循環。

問題是,while循環后沒有括號,因此它僅作用於后面的行。

以下可能是您想要的。

class Program
{
    static void Main(string[] args)
    {
        string Studentname;
        string retry = "No";

        Console.WriteLine("What is the Student's name? ");
        do
        {
            Console.WriteLine("What is the Student's name? ");
            Studentname = Console.ReadLine();

            switch (Studentname)
            {
                case "George":
                    Console.WriteLine("Yes in the list");
                    //Console.ReadLine();
                    break;
                case "Goblin":
                    Console.WriteLine("Yes in the list");
                    //Console.ReadLine();
                    break;
                case "Peter":
                    Console.WriteLine("Yes in the list");
                    //Console.ReadLine();
                    break;
                case "TJ":
                    Console.WriteLine("Yes in the list");
                    //Console.ReadLine();
                    break;
                default:
                    Console.WriteLine("Not in the list");
                    Console.WriteLine("Would you like to retry?");
                    retry = Console.ReadLine();
                    break;
            }
        } while (retry != "No");
    }
}

編輯:您的while循環也從未輸入。 您將“重試”設置為“否”,然后檢查它是否不是“否”。 為了解決這個問題,您可以使用“ do ... while()”循環,該循環始終循環至少一次,或者您可以將“ retry”的第一個分配更改為“ No”以外的任何值。

編輯2:@Kason是正確的。 我沒有意識到您的實際目標是找到名字后退出。 如果是這樣,那么“ do ... while()”是您的最佳選擇。

相反,您可以使用以下簡單的LINQ方法:

string Studentnames[] = new string[]
{
    "George",
    "Goblin",
    "Peter",
    "TJ"
}

while (retry != "No")
{
    if (Studentnames.Contains(Studentname)
    {
        Console.WriteLine("Yes in the list");
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("Not in the list");
        Console.WriteLine("Would you like to retry?");
        retry = Console.ReadLine();
    }
}

但這是行不通的,因為您忘記了將{}放在while語句中。

在while語句之后添加左花括號,並在代碼底部添加匹配的右花括號。

while循環開始之后沒有大括號。 這意味着while語句僅適用於緊接的下一行。

while (retry != "No")
    Console.WriteLine("What is the Student's name? ");

如前所述, retry從“否”開始。 因此,在評估while條件時,它為false。 下一行從不執行,因為僅在retry != "No"時才執行。

當你想while適用於語句塊,把它們放在大括號內。

while (retry != "No")
{
    //Everything inside these braces will execute if retry != "No".
    //If it gets to the end and retry still != "No" then it will repeat.
    //You'd most likely want to do something inside this loop sooner or
    //later that changes retry to something else so that the loop can
    //end. Or you can use "break" to exit the loop.
}

只是改變break; continue;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM