如何在第二次循环后停止我的程序退出?

时间:2015-08-06 23:26:28

标签: c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Excersises
{
class Program
{
    static void Main(string[] args)
    {
        string input;
        bool correct = true;

        Console.WriteLine("Please choose your favorite beverage");
        Console.WriteLine("For Cola type '1'");
        Console.WriteLine("For Sprite type '2'");
        Console.WriteLine("For Fanta, type '3'");
        Console.WriteLine("For Bitter Lemon, type '4'");
        Console.WriteLine("For Beer, type '5'");

        input = Console.ReadLine();

        while (correct)
        {

            if (input == "1")
            {
                Console.WriteLine("Enjoy your Cola!");
                Console.ReadLine();
                correct = true;

            }
            else if (input == "2")
            {
                Console.WriteLine("Enjoy your Sprite!");
                Console.ReadLine();
                correct = true;

            }
            else if (input == "3")
            {
                Console.WriteLine("Enjoy your Fanta!");
                Console.ReadLine();
                correct = true;

            }
            else if (input == "4")
            {
                Console.WriteLine("Enjoy your Bitter Lemon!");
                Console.ReadLine();
                correct = true;

            }
            else if (input == "5")
            {
                Console.WriteLine("Enjoy your beer!");
                Console.ReadLine();
                correct = true;
            }
            else if (input == " ")
            {
                Console.WriteLine("That is not a valid input!");
                Console.ReadLine();
                correct = false;

            }
        }

    }




   }

在我第二次尝试输入后,我完全不知道为什么我的程序会退出。如果我输入" ",它按照预期运行并打印'这不是有效的输入'。但是,之后我无法输入。我已尝试使用while循环来阻止它关闭,但没有成功。我做错了什么?

4 个答案:

答案 0 :(得分:2)

你没有做错任何事。但是您设置correct = false;并且您的程序仅会继续while(correct)。输入无效后,它会停止迭代,找不到任何其他操作,并正确完成。您可能希望将循环上的条件更改为某个退出条件,例如while(continue),然后在continue = false处有特定输入。

编辑:示例

bool workToDo = true;
while(workToDo) {
    Console.WriteLine("Please choose your favorite beverage (and other text)");
    string input = Console.ReadLine();

    if (input == "1") {
        Console.WriteLine("Enjoy your Cola!");
    }
    // Your normal options go here and for the other inputs.
    else if(input == "exit") {
        workToDo = false;
    }
    else {
        Console.WriteLine("That is not a valid input! Try again!");
    }

请注意,我没有使用correct变量。我不知道你是否需要为其他目的检查正确的选项。

答案 1 :(得分:1)

什么是退出条件?根据您的评论,听起来您对无效输入的预期响应是显示错误消息,然后继续请求输入。如果你不希望你的循环退出,只需将(更正)更改为while(true),在这种情况下,你将永远循环。

答案 2 :(得分:1)

围绕correct标志的逻辑是错误的。您应该从bool correct = true;开始,并将while中的逻辑翻转为while (!correct)

您可以尝试这种方法:

string[] beverages = new string[]
{
    "Cola", "Sprite", "Fanta", "Bitter Lemon", "Beer"
};

Console.WriteLine("Please choose your favorite beverage");
for (int i = 0; i < beverages.Length; i++)
{
    Console.WriteLine("For {0} type '{1}'", beverages[i], i + 1);
}

bool correct = false;
while (!correct)
{
    int input = 0;
    if (int.TryParse(Console.ReadLine(), out input))
    {
        correct = Enumerable.Range(1, beverages.Length).Contains(input);
    }
    if (correct)
    {
        Console.WriteLine("Enjoy your {0}!", beverages[input - 1]);
    }
    else
    {
        Console.WriteLine("That is not a valid input!");
    }
}

答案 3 :(得分:0)

I am guessing that your while should be

while(!correct) 

rather than

while(correct)

Just try to read it in English: while request is not correct keep asking what they want and you wrote: while they give me a correct answer I will ask them the same question :)