C# 入门之处理用户的输入

在很多情况下,我们的程序,通过命令终端与用户交互。让用户输入,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.");
            }
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210918/20099494JVEkLSrQhM.png


<<:  Day17 测试写起乃 - expect() vs expect {}

>>:  Powershell 入门之命令的输出

Day5 Game Frontend

今天我们来了解一下 Game Frontend 这个须由我们实作的部件,在 Open-Match 所...

Day 3【NFT】你那边还来得及,赶快 all in Bitcoin

【前言】 在执行这个 Project 之前呢,团队成员有开一次技术会议来详述 Project 的目...

找LeetCode上简单的题目来撑过30天啦(DAY24)

好像有台风要来,天气变得有点冷 题号:24 标题:Swap Nodes in Pairs 难度:Me...

过了一个月,有什麽改变?

没想到写一写就满一个月了,对於新创来说,时间真的是一下子就过了,趁铁人赛的机会能够检视自己团队的成长...