數組實現隨機數並判斷最大值以及位置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shuzujichu
{
    class Program
    {
        static void Main(string[] args)
        {
            //在數組中產生一批隨機數,記錄其中最大數,以及最大數的位置
            const int N = 10;
            int[] a = new int[N];
            int max, max_i;
            Random random = new Random();
            for (int i = 0; i < N; i++)
            {
                a[i] = random.Next(10, 99);
            }
            max = a[0];
            max_i = 0;
            for (int i = 0; i < N; i++)
            {
                if (max < a[i])
                {
                    max = a[i];
                    max_i = i;
                }
            }
                for (int i = 0;i < N;i++)
                {
                    Console.Write("{0}  ",a[i]);
                }
                Console.WriteLine();
                Console.Write("max:{0},location:{1}",max,max_i);
                Console.ReadKey();
            }

        }

    }

效果如圖:

發佈了32 篇原創文章 · 獲贊 8 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章