我试图从控制台获取一个字符串,并将所有元素放在一个int数组中。 它抛出一个错误,我的输入格式错误。我正在尝试" 1 1 3 1 2 2 0 0"我需要那些作为int值,然后用它们执行一些计算。
这是我的尝试:
class Program
{
static void Main()
{
string first = Console.ReadLine();
string[] First = new string[first.Length];
for (int i = 0; i < first.Length; i++)
{
First[i] += first[i];
}
int[] Arr = new int[First.Length];//int array for string console values
for (int i = 0; i < First.Length; i++)//goes true all elements and converts them into Int32
{
Arr[i] = Convert.ToInt32(First[i].ToString());
}
for (int i = 0; i < Arr.Length; i++)//print array to see what happened
{
Console.WriteLine(Arr[i]);
}
}
}
答案 0 :(得分:6)
您需要使用String.Split方法在字符串数组中将字符串与空格' '
分开,然后将每个元素转换为整数。您可以使用System.Linq以有效的方式迭代字符串数组
using System.Linq; //You need add reference to namespace
static void Main()
{
string numbersStr = "1 1 3 1 2 2 0 0";
int[] numbersArrary = numbersStr.Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
}
答案 1 :(得分:3)
答案 2 :(得分:2)
你走了:
class Program
{
static void Main()
{
string numberStr = Console.ReadLine(); // "1 2 3 1 2 3 1 2 ...."
string[] splitted = numberStr.Split(' ');
int[] nums = new int[splitted.Length];
for(int i = 0 ; i < splitted.Length ; i++)
{
nums[i] = int.Parse(splitted[i]);
}
}
}
答案 3 :(得分:1)
尝试将其更改为:
string s = Console.ReadLine();
string[] arr = s.Split(' '); //Split the single string into multiple strings using space as delimiter
int[] intarr = new int[arr.Length];
for(int i=0;i<arr.Length;i++)
{
intarr[i] = int.Parse(arr[i]); //Parse the string as integers and fill the integer array
}
for(int i=0;i<arr.Length;i++)
{
Console.Write(intarr[i]);
}
答案 4 :(得分:1)
您没有使用空格分隔符拆分字符串。
string first = Console.ReadLine();
int len = first.Split(new []
{' '},StringSplitOptions.RemoveEmptyEntries).Length;
string[] First = new string[len];
for (int i = 0; i < len; i++)
{
First[i] = first.Split(' ')[i];
}
int[] Arr = new int[First.Length];//int array for string console values
for (int i = 0; i < First.Length; i++)//goes true all elements and converts them into Int32
{
Arr[i] = Convert.ToInt32(First[i].ToString());
}
for (int i = 0; i < Arr.Length; i++)//print array to see what happened
{
Console.WriteLine(Arr[i]);
}
答案 5 :(得分:1)
您无法尝试使用&#34; 1 1 3 1 2 2 0 0&#34;,因为它正在尝试解析数字之间的空格。如果你想让你的程序工作,你必须要输入你的输入字符串:&#34; 11312200&#34;或者你可以创建一个char数组或只是一个char,如果你没有多个分隔符,你可以通过传递分隔符来分配字符串,然后.split字符串,如下所示:
string Numbers = "1 1 3 1";
string[] seperatedNumbers = Numbers.Split(' ');
// perform your following actions
答案 6 :(得分:0)
您可以使用简单的扩展方法执行此操作:
使用:
var stringArray = "0,1,2,3,4,5";
stringArray.ParseIntArray();
代码:
//separator parameter: default value is ',' you can use whatever you want. ' ', '|', ';'
public static int[] ParseIntArray(this string source, char separator = ',')
{
return Array.ConvertAll(source.Split(separator), int.Parse);
}
如果您不想获得例外,可以使用此功能。
抛出异常时返回new int[] {0}
。
使用:
var stringArray = "0,1,2,3,4,5";
stringArray.TryParseIntArray();
代码:
public static int[] TryParseIntArray(this string source, char separator = ',')
{
int[] arrInt;
try
{
arrInt = Array.ConvertAll(source.Split(separator), int.Parse);
}
catch (Exception ex)
{
arrInt = new int[] { 0 };
}
return arrInt;
}