简体   繁体   English

从单行给出的控制台读取数字,用空格分隔

[英]Read numbers from the console given in a single line, separated by a space

I have a task to read n given numbers in a single line , separated by a space (我的任务是在一行中读取n给定的数字,用空格分隔 ( ) from the console. ) 从控制台。

I know how to do it when I read every number on a separate line ( Console.ReadLine() ) but I need help with how to do it when the numbers are on the same line .当我在单独的行( Console.ReadLine() ) 上读取每个数字时,我知道该怎么做,但是当数字在同一行时,我需要帮助。

You can use String.Split .您可以使用String.Split You can provide the character(s) that you want to use to split the string into multiple.您可以提供要用于将字符串拆分为多个的字符。 If you provide none all white-spaces are assumed as split-characters(so new-line, tab etc):如果你没有提供所有的空格都被假定为分割字符(所以换行,制表符等):

string[] tokens = line.Split(); // all spaces, tab- and newline characters are used

or, if you want to use only spaces as delimiter:或者,如果您只想使用空格作为分隔符:

string[] tokens = line.Split(' ');

If you want to parse them to int you can use Array.ConvertAll() :如果你想将它们解析为int你可以使用Array.ConvertAll()

int[] numbers = Array.ConvertAll(tokens, int.Parse); // fails if the format is invalid

If you want to check if the format is valid use int.TryParse .如果要检查格式是否有效,请使用int.TryParse

You can split the line using String.Split() :您可以使用String.Split()分割线:

var line = Console.ReadLine();
var numbers = line.Split(' ');
foreach(var number in numbers)
{
    int num;
    if (Int32.TryParse(number, out num))
    {
        // num is your number as integer
    }
}

You can use Linq to read the line then split and finally convert each item to integers:您可以使用Linq读取该行然后拆分,最后将每个项目转换为整数:

  int[] numbers = Console
        .ReadLine()
        .Split(new Char[] {' '}, StringSplitOptions.RemoveEmptyEntries)
        .Select(item => int.Parse(item))
        .ToArray();

You simply need to split the data entered.您只需要拆分输入的数据。

string numbersLine = console.ReadLine();

string[] numbers = numbersLine.Split(new char[] { ' '});

// Convert to int or whatever and use

This will help you to remove extra blank spaces present at the end or beginning of the input string.这将帮助您删除输入字符串末尾或开头的额外空格。

string daat1String = Console.ReadLine();
daat1String = daat1String.TrimEnd().TrimStart();
string[] data1 = daat1String.Split(null);
int[] data1Int = Array.ConvertAll(data1, int.Parse);

you can do你可以做

int[] Numbers  = Array.ConvertAll(Console.ReadLine().Split(' '),(item) => Convert.ToInt32(item));

the above line helps us get individual integers in a Line , separated by a Single space .Two Or More spaces between numbers will result in error.上面的行帮助我们在一行中获取单个整数,由单个空格分隔。数字之间的两个或多个空格将导致错误。

int[] Numbers = Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), (item) => Convert.ToInt32(item));

this variation will Fix the error and work well even when two or more spaces between numbers in a Line即使在一行中的数字之间有两个或多个空格,此变体也将修复错误并运行良好

you can use this function, it's very helpful你可以使用这个功能,它很有帮助

    static List<string> inputs = new List<string>();
    static int input_pointer = 0;

    public static string cin(char sep = ' ')
    {
        if (input_pointer >= inputs.Count)
        {
            string line = Console.ReadLine();

            inputs = line.Split(sep).OfType<string>().ToList();
            input_pointer = 0;
        }

        string v = inputs[input_pointer];
        input_pointer++;

        return v;
    }

Example:例子:

        for(var i =0; i<n ; i++)
            for (var j = 0; j<n; j++)
            {
                M[i,j] = Convert.ToInt16(cin());
            }

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

相关问题 在一行中给出一串数字,用空格分隔 - Give a string of numbers on a single line, separated by a space 如何读取由一个输入行的空格分隔的多个输入? - How to Read Multiple Inputs separated by a space from one input line? 快速有效地将空格分隔的数字文件读入数组? - Fast and efficient way to read a space separated file of numbers into an array? 正则表达式+将由空格分隔的数字行转换为数组 - Regex + Convert line of numbers separated by white space into array C#-在同一行上读取2个字符串,在控制台应用程序上用空格隔开 - C# - Read 2 strings on the same line separated by whitespace on a console application 如何在C#中读取以空格分隔或换行符分隔的控制台输入? - How to read console input either space separated or newline separated in C#? 从用户读取整数数组,在C#中以空格分隔 - Read array of integers from user which is separated by space in C# 保存用新行分隔并用空格分隔的值 - Save values separated by new line and separated by space 试图将文本文件中的数据解析为由 | 分隔的单行象征 - Trying to parse data from text file to single line separated by | symbol 如何在C#控制台中的单行中读取矩阵行元素 - how to read matrix row elements in single line in C# console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM