下面是代码草稿:
namespace Ch05Ex07p
{
class Program
{
static int MaxValueInt(int[] doubleArray)
{
int maxVal = doubleArray[0];
for (int i = 1; i < doubleArray.Length; i++)
{
if (doubleArray[i] > maxVal)
maxVal = doubleArray[i];
}
return maxVal;
}
static double MaxValue(double [] doubleArray) //这是求最大值的可以不看
{
double maxVal = doubleArray[0];
for (int i=1;i<doubleArray .Length ;i++)
{
if (doubleArray[i] > maxVal)
maxVal = doubleArray[i];
}
return maxVal ;
}
static double PublicVal (double []doubleArray)
{
double publicVal =doubleArray[0];
int timenum=0;
int []times=new int [doubleArray.Length];
for (int i=0;i<doubleArray .Length ;i++)
{
for (int n = 0; n < doubleArray.Length; n++)
{
if (doubleArray[i] == doubleArray[n])
times[i] = ++times[i];
}
}
timenum=MaxValueInt (times);
for (int x=0;x<times .Length ;x++)
{
if (times[x]==timenum)
return doubleArray[x];
}
return 0;
}
static void Main(string[] args)
{
double maxVal;
double publicVal;
double[] numbers ={ 10, 10, 6, 6, 6, 9, 0 };
maxVal = MaxValue(numbers);
Console.WriteLine("最大值为{0}", maxVal);
publicVal = PublicVal(numbers);
Console.WriteLine("众数为{0}", publicVal);
}
}
}
请大家帮我优化一下,谢谢