日期:2014-05-18 浏览次数:20977 次
Console.Write("请输入你的数组元素,以空格隔开,以回车结束:"); string str = Console.ReadLine(); string [] result= str.Split(' '); int[] myArray = new int[result.Length]; for(int i=0;i<myArray.Length;i++) { myArray[i]=Convert.ToInt32(result[i]); } int maxVal=MaxValue(myArray); Console.WriteLine("The maximum value in myArray is {0}",maxVal); Console.ReadKey();
------解决方案--------------------
class Program
{
static int MaxValue(int[] intArray)
{
int maxVal = intArray[0];
for (int i = 1; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
{
maxVal = intArray[i];
}
}
return maxVal;
}
static void Main(string[] args)
{
Console.Write("请输入你的数组元素,以空格隔开,以回车结束:");
string str = Console.ReadLine();
string[] result = str.Split(' ');
int[] myArray = new int[result.Length];
for (int m = 0; m < result.Length; m++)
{
myArray[m] = Convert.ToInt32(result[m]);
}
//int[] myArray ={ 1, 8, 3, 6, 2, 5, 9, 3, 0, 2 };
Console.WriteLine("数组元素有:");
for (int i = 0; i < myArray.Length; i++)
{
Console.WriteLine("{0}",myArray[i]);
}
int maxVal = MaxValue(myArray);
Console.WriteLine("The maximum value in myArray is {0}", maxVal);
Console.ReadKey();
}
}