use*_*635 6 .net c# arrays math int
我想学习的方法是将一个int数组转换为C#中的int.
但是我想用数组中的值附加int.
例:
int[] array = {5, 6, 2, 4};
Run Code Online (Sandbox Code Playgroud)
将被转换为等于5624的int.
在此先感谢您的帮助.
Dor*_*hen 14
简单地将每个数字乘以10 ^他在数组中的位置.
int[] array = { 5, 6, 2, 4 };
int finalScore = 0;
for (int i = 0; i < array.Length; i++)
{
finalScore += array[i] * Convert.ToInt32(Math.Pow(10, array.Length-i-1));
}
Run Code Online (Sandbox Code Playgroud)
int output = array
.Select((t, i) => t * Convert.ToInt32(Math.Pow(10, array.Length - i - 1)))
.Sum();
Run Code Online (Sandbox Code Playgroud)
另一个简单的方法:
int[] array = {5, 6, 2, 4};
int num;
if (Int32.TryParse(string.Join("", array), out num))
{
//success - handle the number
}
else
{
//failed - too many digits in the array
}
Run Code Online (Sandbox Code Playgroud)
这里的技巧是使数组成为数字字符串,然后将其解析为整数。