我想從控制檯獲取一個字符串,並將所有元素放在一個int數組中。 它引發錯誤,我的輸入格式錯誤。我正在嘗試使用「1 1 3 1 2 2 0 0」,我需要這些值作爲整型值,然後對它們進行一些計算。字符串數組到內部數組
這裏是我的嘗試:
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]);
}
}
}
你應該分配給'的String []'變量在第二行。 –