我收到将Int
转换为Double
的错误,我的下一行Average[y] = Average[y] + Students[i].Marks;
也无效。那么如何将int[]
转换为double[]
?
public static double[] Averages(StudentMarks[] Students, int amount, string[] DifferentGroups, int GroupAmount, out double[] Average)
{
Average = new double[Max];
int y = 0;
int CountGroups = 0;
//int sum = 0;
for (int i = 0; i < amount; i++)
{
if (Students[i].Group == DifferentGroups[y])
{
Convert.ToDouble(Students[i].Marks);
//Average[y] = Average[y] + Students[i].Marks;
}
}
return Average;
}
主要:
static void Main(string[] args)
{
int amount;
StudentMarks[] Students;
string[] DifferentGroups;
int GroupAmount;
double[] Average;
ReadData(out Students, out amount);
DifferentGroups = FindDifferentGroups(Students, amount, out DifferentGroups, out GroupAmount);
Average = Averages(Students, amount, DifferentGroups, GroupAmount, out Average);
类别:
public const int Max = 50;
public string Surname { get; set; }
public string Name { get; set; }
public string Group { get; set; }
public int AmountOfMarks { get; set; }
public int[] Marks { get; set; }
所以学生[i] .Marks是Array。
答案 0 :(得分:1)
Convert.ToDouble
返回一个double - 它不会使整数属性或变量成为double。
我想你想要:
if (Students[i].Group == DifferentGroups[y])
{
double mark = Convert.ToDouble(Students[i].Marks);
Average[y] = Average[y] + mark;
}
或只是:
if (Students[i].Group == DifferentGroups[y])
{
Average[y] = Average[y] + Convert.ToDouble(Students[i].Marks);
}
答案 1 :(得分:0)
所有Convert.ToXXX
个函数返回新值。因此,您应该期望并将返回值分配给局部变量。
类似的东西:
double YourMark = Convert.ToDouble(Students[i].Marks);
答案 2 :(得分:0)
那么如何将int []转换为double []?
通过编程。你编写代码来做到这一点。
var d = new double[Students[i].Marks.Length];
for (int j = 0; j < Students[i].Marks.Length; ++j)
{
d[j] = Convert.ToDouble(Students[i].Marks[j]);
}
或者使用LINQ:
var d = Students[i].Marks.Select(n => Convert.ToDouble(n)).ToArray();
下次方法调用无法按照您希望的方式编译或不能正常工作时,首先要检查the documentation。