从同一行读取数组元素 (C#)
Reading array elements from the same line (C#)
是否可以在 C# 中从同一行(从控制台)读取数组元素?我知道可以从控制台读取多个输入并使用 Split() 将各个部分存储在不同的变量中。但是我不明白如何在数组中做到这一点。
代码
for (int i = 0; i < arrival.Length; i++)
{
arrival[i] = int.Parse(Console.ReadLine());
}
比如我要在数组中输入元素34 35 36 37。如果我使用上面提到的代码,我必须在单独的行中输入每个元素。但我需要的是,如果我在控制台中输入 34 35 36 37,它必须将每个数字存储为数组中的一个元素。如何做到这一点?
我不清楚问题,可能你正在寻找这个
使用系统;
class Program
{
static void Main()
{
string s = "there is a cat";
// Split string on spaces.
// ... This will separate all the words.
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
}
}
输出将是
there
is
a
cat
参考 link - http://www.dotnetperls.com/split
对于整数类型的数组,您可以按照以下方式完成
string readLine=Console.ReadLine());
string[] stringArray=readLine.split(' ');
int[] intArray = new int[stringArray.Length];
for(int i = 0;i < stringArray.Length;i++)
{
// Note that this is assuming valid input
// If you want to check then add a try/catch
// and another index for the numbers if to continue adding the others
intArray[i] = int.parse(stringArray[i]);
}
您需要从控制台读取,拆分输入字符串,将拆分后的字符串转换为您的类型(此处为双精度),然后将它们添加到您自己的数组中:
这是执行您想要的操作的代码:
using System;
using System.Collections.Generic;
using System.Linq;
namespace test4
{
class Program
{
static void Main(string[] args)
{
List<double> arrayOfDouble = new List<double>(); // the array to insert into from console
string strData = Console.ReadLine(); // the data, exmple: 123.32, 125, 78, 10
string[] splittedStrData = strData.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
// trim then parse to souble, then convert to double list
List<double> dataArrayAsDouble = splittedStrData.Select((s) => { return double.Parse(s.Trim()); }).ToList();
// add the read array to your array
arrayOfDouble.AddRange(dataArrayAsDouble);
}
}
}
是否可以在 C# 中从同一行(从控制台)读取数组元素?我知道可以从控制台读取多个输入并使用 Split() 将各个部分存储在不同的变量中。但是我不明白如何在数组中做到这一点。
代码
for (int i = 0; i < arrival.Length; i++)
{
arrival[i] = int.Parse(Console.ReadLine());
}
比如我要在数组中输入元素34 35 36 37。如果我使用上面提到的代码,我必须在单独的行中输入每个元素。但我需要的是,如果我在控制台中输入 34 35 36 37,它必须将每个数字存储为数组中的一个元素。如何做到这一点?
我不清楚问题,可能你正在寻找这个 使用系统;
class Program
{
static void Main()
{
string s = "there is a cat";
// Split string on spaces.
// ... This will separate all the words.
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
}
}
输出将是
there
is
a
cat
参考 link - http://www.dotnetperls.com/split
对于整数类型的数组,您可以按照以下方式完成
string readLine=Console.ReadLine());
string[] stringArray=readLine.split(' ');
int[] intArray = new int[stringArray.Length];
for(int i = 0;i < stringArray.Length;i++)
{
// Note that this is assuming valid input
// If you want to check then add a try/catch
// and another index for the numbers if to continue adding the others
intArray[i] = int.parse(stringArray[i]);
}
您需要从控制台读取,拆分输入字符串,将拆分后的字符串转换为您的类型(此处为双精度),然后将它们添加到您自己的数组中:
这是执行您想要的操作的代码:
using System;
using System.Collections.Generic;
using System.Linq;
namespace test4
{
class Program
{
static void Main(string[] args)
{
List<double> arrayOfDouble = new List<double>(); // the array to insert into from console
string strData = Console.ReadLine(); // the data, exmple: 123.32, 125, 78, 10
string[] splittedStrData = strData.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
// trim then parse to souble, then convert to double list
List<double> dataArrayAsDouble = splittedStrData.Select((s) => { return double.Parse(s.Trim()); }).ToList();
// add the read array to your array
arrayOfDouble.AddRange(dataArrayAsDouble);
}
}
}