[英]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]);
}
}
我希望用戶輸入以下內容:兩個數字,用空格分隔,例如:5 2
之后,我希望能夠在 var one 中捕獲第一個數字,在 var 2 中捕獲第二個數字。 我怎樣才能讓這個程序忽略 2 之后的所有內容? 現在,如果我在 2 之后添加任何其他內容,程序就會崩潰。
您可以通過索引獲取字符,然后使用Char.GetNumericValue
轉換為int
:
var one = (int)Char.GetNumericValue(informatie[0]);
var two = (int)Char.GetNumericValue(informatie[2]);
當然你也應該驗證輸入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.