数组(求最大值及下标)定义一个整型数组,输入10个整数到数组中,

2018-11-29  本文已影响0人  唯一的one

第一种方法:


image.png
image.png
 int[] arr = new int[10];
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("请输入一个数");
                arr[i] = int.Parse(Console.ReadLine());
            }
            int max = -100;
            int index = -1;
            for (int i = 0; i < arr.Length; i++)
            {
                if (max < arr[i])
                {
                    max = arr[i];
                    index = i;
                }
            }
            Console.WriteLine("最大值是{0},下标是{1}", max, index);

第二种方法:


image.png
 int[] arrs = { 7, 8, 5, 12, 36, 2, 9, 1, 3, 4 };
            int max = 0;
            int index = 0;
            for (int i = 0; i < arrs.Length; i++)
            {
                if (arrs[i] > max)
                {
                    index = i;
                    max = arrs[i];
                }
            }
            Console.WriteLine("max={0},index={1}", max, index);
上一篇下一篇

猜你喜欢

热点阅读