停止默认循环

时间:2017-06-12 17:49:44

标签: c# switch-statement

我正在制作一个祈祷时间应用程序,它包含一个基本的switch语句和一个类。我做了一个默认值,这是一个错误,但错误继续循环,因为我有一个while语句。如何在默认情况下停止循环,我想循环下面的switch语句是我的代码 -

while not user_url.startswith("http") and not user_url.startswith("https"):

4 个答案:

答案 0 :(得分:2)

输入默认状态后,您可以将bool设置为true。

示例:

bool end = false;
while (!end)
{
    switch (input)
    {
        // Your switch code

        default:
            end= true;
            Console.WriteLine("Error - Please make sure you're input is similar to the example shown");
            break;

     }
}

答案 1 :(得分:2)

如果我理解你正确想插入这一行

/var/lib/aufs/docker/mnt

进入while循环,每次循环启动时都会为用户提供插入新输入的选项。

答案 2 :(得分:1)

在您的示例中,您可以使用return语句轻松替换break:

default:
    //...
    return;

因为switch语句是方法中的最后一个语句。否则我会把循环放在另一个方法中,你可以再次使用return语句:

while (true)
{
    switch (input)
    {
        //...
        default:
            return;
//...

否则有两种不同的,在我看来不是那些值得推荐的解决方案:

<强> 1。使用带有转到的标签

然而,没有人会建议这样做。它看起来像这样:

while (true)
{
    switch (input)
    {
        //...
        default:
            //...
            goto after;
    }
}
after:
; //The semicolon is only needed when the label would be the last statement in the method otherwise, as it is in your example

<强> 2。使用变量

您可以使用条件循环替换无限循环:

var finished = false;
while (!finished)
{
    switch (input)
    {
        //...
        default:
            //...
            finished = true;
            break;
    }
}

在这种情况下,这是一个相当不错的解决方案。

顺便说一句,你的代码有另一个问题,因为你可能想把输入赋值放在while循环中。总的来说,我建议这样的事情:

var input = Console.ReadLine();

while (true)
{
    input = Console.ReadLine();
    switch (input)
    {
        case "6/12/2017":
        case "06/12/2017":
            Console.WriteLine("Fajr - 3:32 AM");
            Console.WriteLine("Sunrise - 5:14 AM");
            Console.WriteLine("Dhuhr - 12:51 PM");
            Console.WriteLine("Asr - 4:53 PM");
            Console.WriteLine("Maghrib - 8:27 PM");
            Console.WriteLine("Isha - 10:10 PM");
            Console.WriteLine();
            break;

        case "6/13/2017":
        case "06/13/2017":
            Console.WriteLine("Fajr - 3:32 AM");
            Console.WriteLine("Sunrise - 5:14 AM");
            Console.WriteLine("Dhuhr - 12:51 PM");
            Console.WriteLine("Asr - 4:54 PM");
            Console.WriteLine("Maghrib - 8:27 PM");
            Console.WriteLine("Isha - 10:10 PM");
            Console.WriteLine();
            break;

        case "6/14/2017":
        case "06/14/2017":
            Console.WriteLine("Fajr - 3:32 AM");
            Console.WriteLine("Sunrise - 5:14 AM");
            Console.WriteLine("Dhuhr - 12:51 PM");
            Console.WriteLine("Asr - 4:54 PM");
            Console.WriteLine("Maghrib - 8:28 PM");
            Console.WriteLine("Isha - 10:11 PM");
            Console.WriteLine();
            break;

        case "6/15/2017":
        case "06/15/2017":
            Console.WriteLine("Fajr - 3:32 AM");
            Console.WriteLine("Sunrise - 5:14 AM");
            Console.WriteLine("Dhuhr - 12:52 PM");
            Console.WriteLine("Asr - 4:54 PM");
            Console.WriteLine("Maghrib - 8:28 PM");
            Console.WriteLine("Isha - 10:11 PM");
            Console.WriteLine();
            break;

        case "6/16/2017":
        case "06/16/2017":
            Console.WriteLine("Fajr - 3:32 AM");
            Console.WriteLine("Sunrise - 5:14 AM");
            Console.WriteLine("Dhuhr - 12:52 PM");
            Console.WriteLine("Asr - 4:55 PM");
            Console.WriteLine("Maghrib - 8:28 PM");
            Console.WriteLine("Isha - 10:12 PM");
            Console.WriteLine();
            break;

        case "6/17/2017":
        case "06/17/2017":
            Console.WriteLine("Fajr - 3:32 AM");
            Console.WriteLine("Sunrise - 5:14 AM");
            Console.WriteLine("Dhuhr - 12:52 PM");
            Console.WriteLine("Asr - 4:55 PM");
            Console.WriteLine("Maghrib - 8:29 PM");
            Console.WriteLine("Isha - 10:12 PM");
            Console.WriteLine();
            break;

        case "6/18/2017":
        case "06/18/2017":
            Console.WriteLine("Fajr - 3:32 AM");
            Console.WriteLine("Sunrise - 5:15 AM");
            Console.WriteLine("Dhuhr - 12:52 PM");
            Console.WriteLine("Asr - 4:55 PM");
            Console.WriteLine("Maghrib - 8:29 PM");
            Console.WriteLine("Isha - 10:13 PM");
            Console.WriteLine();
            break;

        case "6/19/2017":
        case "06/19/2017":
            Console.WriteLine("Fajr - 3:32 AM");
            Console.WriteLine("Sunrise - 5:15 AM");
            Console.WriteLine("Dhuhr - 12:52 PM");
            Console.WriteLine("Asr - 4:55 PM");
            Console.WriteLine("Maghrib - 8:29 PM");
            Console.WriteLine("Isha - 10:13 PM");
            Console.WriteLine();
            break;

        case "6/20/2017":
        case "06/20/2017":
            Console.WriteLine("Fajr - 3:32 AM");
            Console.WriteLine("Sunrise - 5:15 AM");
            Console.WriteLine("Dhuhr - 12:53 PM");
            Console.WriteLine("Asr - 4:55 PM");
            Console.WriteLine("Maghrib - 8:30 PM");
            Console.WriteLine("Isha - 10:13 PM");
            Console.WriteLine();
            break;

        case "6/21/2017":
        case "06/21/2017":
            Console.WriteLine("Fajr - 3:32 AM");
            Console.WriteLine("Sunrise - 5:15 AM");
            Console.WriteLine("Dhuhr - 12:53 PM");
            Console.WriteLine("Asr - 4:56 PM");
            Console.WriteLine("Maghrib - 8:30 PM");
            Console.WriteLine("Isha - 10:13 PM");
            Console.WriteLine();
            break;

        case "6/22/2017":
        case "06/22/2017":
            Console.WriteLine("Fajr - 3:33 AM");
            Console.WriteLine("Sunrise - 5:15 AM");
            Console.WriteLine("Dhuhr - 12:53 PM");
            Console.WriteLine("Asr - 4:56 PM");
            Console.WriteLine("Maghrib - 8:30 PM");
            Console.WriteLine("Isha - 10:14 PM");
            Console.WriteLine();
            break;

        case "6/23/2017":
        case "06/23/2017":
            Console.WriteLine("Fajr - 3:33 AM");
            Console.WriteLine("Sunrise - 5:15 AM");
            Console.WriteLine("Dhuhr - 12:53 PM");
            Console.WriteLine("Asr - 4:56 PM");
            Console.WriteLine("Maghrib - 8:30 PM");
            Console.WriteLine("Isha - 10:13 PM");
            Console.WriteLine();
            break;

        case "6/24/2017":
        case "06/24/2017":
            Console.WriteLine("Fajr - 3:33 AM");
            Console.WriteLine("Sunrise - 5:16 AM");
            Console.WriteLine("Dhuhr - 12:52 PM");
            Console.WriteLine("Asr - 4:56 PM");
            Console.WriteLine("Maghrib - 8:30 PM");
            Console.WriteLine("Isha - 10:14 PM");
            Console.WriteLine();
            break;

        default:
            Console.WriteLine("Error - Please make sure you're input is similar to the example shown");
            return;
    }

}

答案 3 :(得分:0)

While (true) 

这是无限循环。您必须使用变量才能完成此操作。

但是。我认为您希望在默认情况下获得用户的输入。

所以你的代码应该是这样的:

bool switchItWhenDone= true;
while (switchItWhenDone)
{
    string input = Console.ReadLine();
    switch (input)
    {
        case "6/12/2017":
        case "06/12/2017":
            Console.WriteLine("Fajr - 3:32 AM");
            Console.WriteLine("Sunrise - 5:14 AM");
            Console.WriteLine("Dhuhr - 12:51 PM");
            Console.WriteLine("Asr - 4:53 PM");               
            Console.WriteLine("Maghrib - 8:27 PM");
            Console.WriteLine("Isha - 10:10 PM");
            Console.WriteLine();
            break;

        case "6/13/2017":
        case "06/13/2017":
        Console.WriteLine("Fajr - 3:32 AM");
        Console.WriteLine("Sunrise - 5:14 AM");
        Console.WriteLine("Dhuhr - 12:51 PM");
        Console.WriteLine("Asr - 4:54 PM");
        Console.WriteLine("Maghrib - 8:27 PM");
        Console.WriteLine("Isha - 10:10 PM");
        Console.WriteLine();
        break;

    case "6/14/2017":
    case "06/14/2017":
        Console.WriteLine("Fajr - 3:32 AM");
        Console.WriteLine("Sunrise - 5:14 AM");
        Console.WriteLine("Dhuhr - 12:51 PM");
        Console.WriteLine("Asr - 4:54 PM");
        Console.WriteLine("Maghrib - 8:28 PM");
        Console.WriteLine("Isha - 10:11 PM");
        Console.WriteLine();
        break;

    case "6/15/2017":
    case "06/15/2017":
        Console.WriteLine("Fajr - 3:32 AM");
        Console.WriteLine("Sunrise - 5:14 AM");
        Console.WriteLine("Dhuhr - 12:52 PM");
        Console.WriteLine("Asr - 4:54 PM");
        Console.WriteLine("Maghrib - 8:28 PM");
        Console.WriteLine("Isha - 10:11 PM");
        Console.WriteLine();
        break;

    case "6/16/2017":
    case "06/16/2017":
        Console.WriteLine("Fajr - 3:32 AM");
        Console.WriteLine("Sunrise - 5:14 AM");
        Console.WriteLine("Dhuhr - 12:52 PM");
        Console.WriteLine("Asr - 4:55 PM");
        Console.WriteLine("Maghrib - 8:28 PM");
        Console.WriteLine("Isha - 10:12 PM");
        Console.WriteLine();
        break;

    case "6/17/2017":
    case "06/17/2017":
        Console.WriteLine("Fajr - 3:32 AM");
        Console.WriteLine("Sunrise - 5:14 AM");
        Console.WriteLine("Dhuhr - 12:52 PM");
        Console.WriteLine("Asr - 4:55 PM");
        Console.WriteLine("Maghrib - 8:29 PM");
        Console.WriteLine("Isha - 10:12 PM");
        Console.WriteLine();
        break;

    case "6/18/2017":
    case "06/18/2017":
        Console.WriteLine("Fajr - 3:32 AM");
        Console.WriteLine("Sunrise - 5:15 AM");
        Console.WriteLine("Dhuhr - 12:52 PM");
        Console.WriteLine("Asr - 4:55 PM");
        Console.WriteLine("Maghrib - 8:29 PM");
        Console.WriteLine("Isha - 10:13 PM");
        Console.WriteLine();
        break;

    case "6/19/2017":
    case "06/19/2017":
        Console.WriteLine("Fajr - 3:32 AM");
        Console.WriteLine("Sunrise - 5:15 AM");
        Console.WriteLine("Dhuhr - 12:52 PM");
        Console.WriteLine("Asr - 4:55 PM");
        Console.WriteLine("Maghrib - 8:29 PM");
        Console.WriteLine("Isha - 10:13 PM");
        Console.WriteLine();
        break;

    case "6/20/2017":
    case "06/20/2017":
        Console.WriteLine("Fajr - 3:32 AM");
        Console.WriteLine("Sunrise - 5:15 AM");
        Console.WriteLine("Dhuhr - 12:53 PM");
        Console.WriteLine("Asr - 4:55 PM");
        Console.WriteLine("Maghrib - 8:30 PM");
        Console.WriteLine("Isha - 10:13 PM");
        Console.WriteLine();
        break;

    case "6/21/2017":
    case "06/21/2017":
        Console.WriteLine("Fajr - 3:32 AM");
        Console.WriteLine("Sunrise - 5:15 AM");
        Console.WriteLine("Dhuhr - 12:53 PM");
        Console.WriteLine("Asr - 4:56 PM");
        Console.WriteLine("Maghrib - 8:30 PM");
        Console.WriteLine("Isha - 10:13 PM");
        Console.WriteLine();
        break;

    case "6/22/2017":
    case "06/22/2017":
        Console.WriteLine("Fajr - 3:33 AM");
        Console.WriteLine("Sunrise - 5:15 AM");
        Console.WriteLine("Dhuhr - 12:53 PM");
        Console.WriteLine("Asr - 4:56 PM");
        Console.WriteLine("Maghrib - 8:30 PM");
        Console.WriteLine("Isha - 10:14 PM");
        Console.WriteLine();
        break;

    case "6/23/2017":
    case "06/23/2017":
        Console.WriteLine("Fajr - 3:33 AM");
        Console.WriteLine("Sunrise - 5:15 AM");
        Console.WriteLine("Dhuhr - 12:53 PM");
        Console.WriteLine("Asr - 4:56 PM");
        Console.WriteLine("Maghrib - 8:30 PM");
        Console.WriteLine("Isha - 10:13 PM");
        Console.WriteLine();
        break;

    case "6/24/2017":
    case "06/24/2017":
        Console.WriteLine("Fajr - 3:33 AM");
        Console.WriteLine("Sunrise - 5:16 AM");
        Console.WriteLine("Dhuhr - 12:52 PM");
        Console.WriteLine("Asr - 4:56 PM");
        Console.WriteLine("Maghrib - 8:30 PM");
        Console.WriteLine("Isha - 10:14 PM");
        Console.WriteLine();
        break;

    default:
        Console.WriteLine("Error - Please make sure you're input is similar to the example shown");
switchItWhenDone=false;
        break;
}

}