[英]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.