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

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

作者: 唯一的one | 来源:发表于2018-11-29 20:05 被阅读0次

第一种方法:


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);

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/ugcqcqtx.html