简体   繁体   English

如何忽略 C# 控制台中前几个字符后的所有输入?

[英]How do I ignore every input after the first few characters in the console in C#?

class Program
    {
        static void Main(string[] args)
        {
            var information= Console.ReadLine();
            var one= Int32.Parse(informatie.Split(' ')[0]);
            var two = Int32.Parse(informatie.Split(' ')[1]);
        }
    }

I want the user to input the following: two numbers, seperated by a whitespace, so, for example: 5 2我希望用户输入以下内容:两个数字,用空格分隔,例如:5 2

After that, I want to be able to catch the first number in var one and the second one in var two.之后,我希望能够在 var one 中捕获第一个数字,在 var 2 中捕获第二个数字。 How can I make this program such that everything which comes after the 2 will be ignored?我怎样才能让这个程序忽略 2 之后的所有内容? Right now, if I add anything else after the 2, the program crashes.现在,如果我在 2 之后添加任何其他内容,程序就会崩溃。

You can get the characters by index, then use Char.GetNumericValue to convert to int :您可以通过索引获取字符,然后使用Char.GetNumericValue转换为int

var one = (int)Char.GetNumericValue(informatie[0]);
var two = (int)Char.GetNumericValue(informatie[2]);

Of course you should also validate the input.当然你也应该验证输入。

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

相关问题 在C#中搜索字符串中每个单词的前几个字符 - Searching the first few characters of every word within a string in C# 如何在 C# 的控制台输入 & output 日语字符(假名)? - How do I input & output Japanese characters (kana) to the console in C#? 如何限制控制台输入的字符数? C# - How can I limit the number of characters for a console input? C# 如何每隔几秒钟截取一次屏幕截图 C# 控制台 - How to take screenshot every few seconds C# console C#如何使开关忽略无效输入? - C# How do I make a switch ignore invalid input? C#-控制台如何获取每一帧的输入 - C# - Console How to get input every frame C#控制台应用程序-如何始终从控制台读取输入? - C# Console Application - How do I always read input from the console? Unity2D / C#:如何使光线投射忽略它的第一次碰撞? - Unity2D/C#: How do I make a raycast ignore its first collision? 如何防止C#控制台应用程序中的Ctrl键生成特殊字符? - How do I prevent special characters from being generated by the Ctrl key in a C# console application? C#.NET Process类:如何将输入发送到正在等待输入的控制台进程? - C# .NET Process class: How do I send input to a console process that is waiting for input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM