如何将字符串的“数组”插入到值中?

我只想输入以下数字


100 8

15 245

1945 54

进入一个值,但由于某种原因,当我将其复制粘贴到我的ReadLine中时 - 程序将我踢出(没有错误或smth - 所以我几乎无法理解刚刚发生的事情...)


我已经有一个代码,允许我在排列在LINE中时插入一组数字(但不是描述中所示的表格...)


        int numberOfElements = Convert.ToInt32(Console.ReadLine());   

        int sum = 0;

        string input = Console.ReadLine();     

        var numbers = Array.ConvertAll(input.Split(' '), int.Parse).ToList();   



        Console.ReadKey();

我希望将我的号码列入列表


芜湖不芜
浏览 117回答 3
3回答

桃花长相依

Console.ReadLine()只读取一行。 读取第一行 当您进入新行时。在你的情况下,只读取第一行,然后对于第二行,你的程序只得到第一个字符并退出。string input = Console.ReadLine()检查这个:&nbsp; &nbsp; int numberOfElements = Convert.ToInt32(Console.ReadLine());&nbsp; &nbsp;&nbsp; &nbsp; int sum= 0;&nbsp; &nbsp; for (int i=0; i< numberOfElements; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string input = Console.ReadLine();&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; sum += Array.ConvertAll(input.Split(' '), int.Parse).Sum();&nbsp; &nbsp; }&nbsp; &nbsp; Console.WriteLine(sum);

守候你守候我

显然,当您粘贴回车符时,只占用到第一个回车符,您将需要一些描述的循环ReadLineint numberOfElements = Convert.ToInt32(Console.ReadLine());var sb = new StringBuilder();for (int i = 0; i < numberOfElements; i++){&nbsp; &nbsp;Console.WriteLine($"Enter value {i+1}");&nbsp; &nbsp;sb.AppendLine(Console.ReadLine());}var input = sb.ToString();// do what ever you want hereConsole.ReadKey();

倚天杖

我假设您正在寻找一种方法来允许用户将其他源中的某些内容粘贴到控制台程序中,因此您正在寻找一个答案,您可以在其中处理来自用户的多行字符串输入(他们在其中粘贴包含一个或多个换行符的字符串)。如果是这种情况,则执行此操作的一种方法是在第一次调用后检查 的值,以查看缓冲区中是否还有更多输入,如果有,请将其添加到已捕获的输入中。Console.KeyAvailableReadLine例如,下面是一个方法,该方法接受提示(向用户显示),然后返回一个,其中包含用户粘贴(或键入)的每一行的条目:List<string>private static List<string> GetMultiLineStringFromUser(string prompt){&nbsp; &nbsp; Console.Write(prompt);&nbsp; &nbsp; // Create a list and add the first line to it&nbsp; &nbsp; List<string> results = new List<string> { Console.ReadLine() };&nbsp; &nbsp; // KeyAvailable will return 'true' if there is more input in the buffer&nbsp; &nbsp; // so we keep adding the lines until there are none left&nbsp; &nbsp; while(Console.KeyAvailable)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; results.Add(Console.ReadLine());&nbsp; &nbsp; }&nbsp; &nbsp; // Return the list of lines&nbsp; &nbsp; return results;}在使用中,这可能看起来像这样:private static void Main(){&nbsp; &nbsp; var input = GetMultiLineStringFromUser("Paste a multi-line string and press enter: ");&nbsp; &nbsp; Console.WriteLine("\nYou entered: ");&nbsp; &nbsp; foreach(var line in input)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(line);&nbsp; &nbsp; }&nbsp; &nbsp; GetKeyFromUser("\nDone!\nPress any key to exit...");}输出你接下来做什么取决于你想要完成什么。如果要获取所有行,将它们拆分为空格字符,并将所有结果作为单个整数的列表返回,则可以执行以下操作:private static void Main(){&nbsp; &nbsp; int temp = 0;&nbsp; &nbsp; List<int> numbers =&nbsp; &nbsp; &nbsp; &nbsp; GetMultiLineStringFromUser("Paste a multi-line string and press enter: ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .SelectMany(i => i.Split()) // Get all the individual entries&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(i => int.TryParse(i, out temp)) // Where the entry is an int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(i => Convert.ToInt32(i)) // And convert the entry to an int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToList();&nbsp; &nbsp; Console.WriteLine("\nYou entered: ");&nbsp; &nbsp; foreach (var number in numbers)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(number);&nbsp; &nbsp; }&nbsp; &nbsp; GetKeyFromUser("\nDone!\nPress any key to exit...");}输出或者你甚至可以做一些花哨的事情,比如:Console.WriteLine($"\n{string.Join("&nbsp;+&nbsp;",&nbsp;numbers)}&nbsp;=&nbsp;{numbers.Sum()}");输出
打开App,查看更多内容
随时随地看视频慕课网APP