繁体   English   中英

如何在第二个循环后阻止程序退出?

[英]How can I stop my program from quitting after second loop?

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循环以防止它关闭,但是没有成功。 我做错了什么?

你没做错什么 但是您设置的是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变量。 我不知道您是否需要出于其他目的检查正确的选项。

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

您围绕correct标志的逻辑是错误的。 您应该以bool correct = true; 并将逻辑翻转为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!");
    }
}

我想你的时间应该是

while(!correct) 

而不是

while(correct)

只需尝试用英语阅读:当请求不正确时,继续询问他们想要什么,您就写道:当他们给我正确答案时,我会问他们同样的问题:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM