日期:2014-05-18  浏览次数:20808 次

输入10个数,求出最大值和最小值
输入10个数,求出最大值和最小值,怎么写
C# code

int[] nums = new int[10];
            for (int i = 0; i < 10; i++)
            {
                System.Console.WriteLine("请输入第{0}个数:", i + 1);
                nums[i] = System.Int32.Parse(System.Console.ReadLine());
            }//输入10个数
            
            for (int k = 0; k < 10; k++)
            {
                int temp = nums[k];
                for (int i = k + 1; i < 10; i++)
                {
                    if (temp > nums[i])
                    {
                        temp = nums[i];
                        nums[i] = nums[k];
                        nums[k] = temp;
                    }

                }
            }

            System.Console.WriteLine();//空行
            for (int j = 0; j < 10; j++)
            {
               
                System.Console.WriteLine(nums[j]);
            }




------解决方案--------------------
int[] nums = new int[10];
int temp=0;

for (int i = 0; i < 10; i++)
{
System.Console.WriteLine("请输入第{0}个数:", i + 1);
nums[i] = System.Int32.Parse(System.Console.ReadLine());
}//输入10个数

for (int k = 0; k < 10; k++)
{

for (int i = k + 1; i < 10; i++)
{
if (num[k] < nums[i])
{
temp=num[k];
num[k]=num[i];
num[i]=temp
}
}

}
c
}

System.Console.WriteLine();//空行
for (int j = 0; j < 10; j++)
{

System.Console.WriteLine(nums[j]);
}
System.Console.WriteLine();//空行

System.Console.WriteLine("Min = "+ nums[j-1]);
System.Console.WriteLine("Max = "+nums[0]);


------解决方案--------------------
C# code

int[] NumArry = {10,11,3,4,5,6,7,8,2,12};
//求最大
for (int i = 1; i < NumArry.Length; i++)
{
   int maxNum = NumArry[0];
   if (NumArry[i] > maxNum) //循环判断大于
   maxNum = NumArry[i];
}
Console.WriteLine(maxNum);
//求最小
for (int i = 1; i < NumArry.Length; i++)
{
   int minNum = intArr[0];
   if (NumArry[i] < minNum) //循环判断小于
   minNum = NumArry[i];
}
Console.WriteLine(minNum);