在很多情况下,我们的程序,通过命令终端与用户交互。让用户输入,yes 或 no 是一种很常见的场景。
今天我们就通过这个示例,来演示处理用户的输入。
下面先看一下示例代码:
using System;
using System.Collections.Generic;
namespace ConsoleApp18
{
class Program
{
//定义一个列表,表示用户可能会输入的值
static List<string> enter_yes = new List<string>() { " ", "yes", "y" };
static List<string> enter_no = new List<string>() { " ", "no", "n" };
static void Main(string[] args)
{
Console.WriteLine("Please enter 'Yes' or 'No'");
string userEnter = Console.ReadLine();
userEnter = userEnter.ToLower(); // 为了减少列表中的元素,对用户输入进行转换
if (enter_yes.Contains(userEnter)) // 调用列表的 Contains 属性,进行逻辑判断
{
Console.WriteLine("Continue run.");
}
else if (enter_no.Contains(userEnter))
{
Console.WriteLine("Stop run.");
}
else
{
Console.WriteLine("enter error.");
}
}
}
}
运行结果:
<<: Day17 测试写起乃 - expect() vs expect {}
今天我们来了解一下 Game Frontend 这个须由我们实作的部件,在 Open-Match 所...
【前言】 在执行这个 Project 之前呢,团队成员有开一次技术会议来详述 Project 的目...
create table it201011a ( gal text not null ); -- ...
好像有台风要来,天气变得有点冷 题号:24 标题:Swap Nodes in Pairs 难度:Me...
没想到写一写就满一个月了,对於新创来说,时间真的是一下子就过了,趁铁人赛的机会能够检视自己团队的成长...