我想创建一个increment_Num[]
的数组,像这样:[1.60,1.62,1.64,1.66,1.68,1.70]
//First step I converted the string to decimal value here:
decimal Start_Num = decimal.Parse("1.60");
decimal Stop_Num = decimal.Parse("1.70");
decimal Steps_Num = decimal.Parse("0.02");
//Second step I calculated the total number of points and converted the decimal value to int here:
decimal steps = (Stop_Num - Start_Num) /Steps_Num;
int steps_int=(int)decimal.Ceiling(steps);
//Third step I tried to create a for loop which will create an array
decimal[] increment_Num = new decimal[steps_int+1];
for (decimal f=0; f<steps_int+1; f+=Steps_Num)
{
increment_Num[f] = Start_Num + f * Steps_Num;
}
下面的代码在increment_Num[f]
的最后第二行第3步给出了这个错误:
错误CS0266不能隐式地将类型"decimal"转换为"int"。存在显式转换(您是否缺少强制类型转换?)
我在声明中做错了什么吗?
张贴我的评论作为答案:
increment_Num[f]
u使用f
作为索引,数组用int索引,f是十进制
代码应该是
for (int index =0; index<=steps_int; index++)
{
increment_Num[index] = Start_Num + index * Steps_Num;
}