使用回车键或值关闭程序

时间:2018-03-21 13:14:39

标签: c#

所以,我的程序工作得很好,但我不知道如何使用回车键关闭它或返回一个不像(y,n)这样的名称的值。我没有看到一个解决方案,可以很好地写入我的代码而不会给我带来错误,所以我觉得由于我的经验不足可能会出现格式错误。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="testSelect" disabled="true">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
</select>
<div class="dirty-hack"></div>

2 个答案:

答案 0 :(得分:0)

string names = Console.ReadLine();添加以下内容后

if(string.IsNullOrEmpty(names) || names == "y" /* and so on */ )
{
    break;
}

如果输入为空,则将退出无限循环while方法,或者为空字符串(输入)或y

答案 1 :(得分:0)

根据您的要求,这可能不正确。但你当然可以尝试这个想法:

private static void Main(string[] args)
    {
        var rollcall = new List<string>();

        while (true)
        {
            Console.WriteLine("Enter something here. May be a number");
            var names = Console.ReadLine();
            if (string.IsNullOrEmpty(names))
            {
                Console.WriteLine("No input captured. Do you want to exit ?. Press enter to exit.");
                if (ConsoleKey.Enter == Console.ReadKey().Key)
                {
                    break;
                }
            }
            rollcall.Add(names);
            var number = rollcall.Count;

            if (number == 1)
            {
                Console.WriteLine(" {0} likes your post.", rollcall[0]);
                continue;
            }
            else if (number == 2)
            {
                Console.WriteLine("{0} and {1} likes your post\n Press Enter to Exit", rollcall[0], rollcall[1]);
                if (ConsoleKey.Enter == Console.ReadKey().Key)
                {
                    break;
                }

                continue;
            }
            else if (number == 3)
            {
                Console.WriteLine("{0}, {1}, and {2} other likes your post.\n Press Enter to Exit", rollcall[0], rollcall[1], number - 2);
                if (ConsoleKey.Enter == Console.ReadKey().Key)
                {
                    break;
                }
                continue;
            }
            else if (number >= 4)
            {
                Console.WriteLine("{0}, {1}, and {2} others like your post.\n Press Enter to Exit", rollcall[0], rollcall[1], number - 2);
                if (ConsoleKey.Enter == Console.ReadKey().Key)
                {
                    break;
                }
                continue;
            }
        }

        Console.WriteLine("Done !");
    }