在C#中编写一个控制台应用程序,以在数组中查找索引i,该索引是数组中的最大数字.
如果数组中的最大元素出现多次,则需要显示最小索引.
如果数组为空,则输出-1.
请告诉我我的代码有什么问题?
a = { 1, 2, 46, 14, 64, 64 };例如,如果我输入数组,它将返回0,而它应该返回 4.
public static void Main()
{
double[] a = { 1, 9, 9, 8, 9, 2, 2 };
Console.WriteLine(MaxIndex(a));
}
public static double MaxIndex(double[] array)
{
var max = double.MinValue;
int maxInd = 0, maxCount = 0;
int minIndex = 0;
var min = double.MaxValue;
for (var i = 0; i < array.Length; i++)
{
if (min > array[i])
{ …Run Code Online (Sandbox Code Playgroud)