using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;//[红色标注:一直疑问这句,在后面的输出中不加Console,会一直报错]
namespace Ch07Ex01
{
class Program
{
static void Main(string[] args)
{
int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };
int[] maxValIndices;
int maxVal = Maxima(testArray, out maxValIndices);
WriteLine($"Maximum value {maxVal} found at element indices:");//此处的$是什么个意思?WriteLine前没有Console.会报错,求解?
foreach (int index in maxValIndices)
{
WriteLine(index);//会报错
}
ReadKey();//会报错
}
static int Maxima(int[] integers, out int[] indices)
{
Debug.WriteLine("Maximum value search started.");
indices = new int[1];
int maxVal = integers[0];
indices[0] = 0;
int count = 1;
Debug.WriteLine(string.Format($"Maximum value initialized to {maxVal}, at element index 0."));//报错
for (int i = 1; i < integers.Length; i++)
{
Debug.WriteLine(string.Format($"Now looking at element at index {i}."));//会报错
if (integers[i] > maxVal)
{
maxVal = integers[i];
count = 1;
indices = new int[1];
indices[0] = i;
Debug.WriteLine(string.Format($"New maximum found. New value is {maxVal}, at element index {i}."));//会报错
}
else
{
if (integers[i] == maxVal)
{
count++;
int[] oldIndices = indices;
indices = new int[count];
oldIndices.CopyTo(indices, 0);
indices[count - 1] = i;
Debug.WriteLine(string.Format($"Duplicate maximum found at element index {i}."));//会报错
}
}
}
Trace.WriteLine(string.Format(
$"Maximum value {maxVal} found, with {count} occurrences."));//会报错
Debug.WriteLine("Maximum value search completed.");
return maxVal;
}
}
}