码迷,mamicode.com
首页 > 编程语言 > 详细

用C#语言编写:数组分析器

时间:2018-03-24 14:28:59      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:输出   main   对组   i+1   class   c#语言   region   reg   查找   

 static void Main(string[] args)
        {
            #region 创建数组
            Console.Write("请输入数组的长度");
            int[] numbers = new int[int.Parse(Console.ReadLine())];
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.Write("请输入数组的第" + (i + 1) + "项:");
                numbers[i] = int.Parse(Console.ReadLine());
            }
            Console.Clear();
            #endregion
            #region  对组数排序
            for (int i = 0; i < numbers.Length-1; i++)
            {
                for (int j = i+1; j < numbers.Length; j++)
                {
                    if (numbers[i] > numbers[j])
                    {
                        int temp = numbers[i];
                        numbers[i] = numbers[j];
                        numbers[j] = temp;
                    }
                    
                }
            }
            Console.WriteLine("您输入的数字排序后如下:");
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.Write(numbers[i]+"\t");
            }
            Console.WriteLine();
            #endregion
            #region  找出奇数
            Console.WriteLine("其中,以下数字是奇数:");
            //查找问题
            //查找样本:数组中所有的数字
            //查找目标:样本数据除以2不为0
            //找到后的操作:输出目标数据
            for (int i = 0; i < numbers.Length; i++)
            {
                int n = numbers[i];
                if (n % 2 != 0)
                {
                    Console.Write(n + "\t");
                }
            }
            Console.WriteLine();

            #endregion
            #region  找出质数
            Console.WriteLine("以下数字式质数:");
            for (int i = 0; i < numbers.Length; i++)
            {
                int n = numbers[i];
                bool isfind = false;
                for (int j = 2; j < n; j++)
                {
                    if (n % j == 0)
                    {
                        isfind = true;
                        break;
                    }
                }
                        if(!isfind)
                        {
                        {
                            Console.Write(n+"\t");
                        }
                }
            }
            #endregion
        }

用C#语言编写:数组分析器

标签:输出   main   对组   i+1   class   c#语言   region   reg   查找   

原文地址:https://www.cnblogs.com/Maigo/p/8638606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!