C#练习——统计整型数组中不重复数字的个数、将普通日期格式转换成汉字日期格式、在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数

来源:互联网 发布:淘宝直播开通后怎么办 编辑:程序博客网 时间:2024/05/16 17:54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ReviewPractice
{
    class Program
    {
        static void Main(string[] args)
        {
            #region  1、请统计出数组:{1,2,3,4,5,6,7,8,9,1,2,3,79,23,45,64,9,3,2,4}中的不重复的数字的个数。2、将数组中重复数字去掉后放入一个新的数组中
            #region  1、统计数组中不重复数字的个数
            //int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 79, 23, 45, 64, 9, 3, 2, 4 };
            ////统计数组中不重复数字的个数
            //int count = 0;
            //for (int i = 0; i < array.Length; i++)
            //{
            //    //判断是否重复
            //    bool b = true;
            //    for (int j = 0; j < array.Length; j++)
            //    {
            //        //假如在不同的索引位置两个数相等
            //        if (array[i] == array[j] && i != j)
            //        {
            //            b = false;
            //            break;
            //        }
            //    }
            //    //假如不重复
            //    if(b)
            //    {
            //        count++;
            //    }
            //}
            //Console.WriteLine("不重复数字的个数为:{0}",count);
            //Console.ReadKey();
            #endregion


            #region  2、将重复数字去掉并放入一个新的数组中
            //int[] arrayInt = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 79, 23, 45, 64, 9, 3, 2, 4 };
            ////对数组进行排序
            //Array.Sort(arrayInt);
            //List<int> list = new List<int>();
            //for (int i = 0; i < arrayInt.Length - 1; i++)
            //{
            //    if (arrayInt[i] != arrayInt[i + 1])
            //    {
            //        list.Add(arrayInt[i]);
            //    }
            //}
            //list.Add(arrayInt[arrayInt.Length - 1]);
            //for (int i = 0; i < list.Count; i++)
            //{
            //    Console.WriteLine(list[i]);
            //}
            //Console.ReadKey();
            #endregion
            #endregion


            #region  制作一个控制台小程序。要求:用户可以在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数,以及每个学生的姓名。再增加一个显示姓“王”的同学的个数,此处不考虑复姓问题。
            //List<string> strList = new List<string>();
            ////统计姓王的同学的个数
            //int count = 0;
            //string name;
            //do
            //{
            //    Console.WriteLine("请输入学生姓名:");
            //    name = Console.ReadLine();
            //    strList.Add(name);
            //    //如果名字中第一个字符是王
            //    if (name.IndexOf('王') == 0)
            //    {
            //        count++;
            //    }
            //}
            //while (name.ToLower() != "quit");
            ////去掉quit
            //strList.RemoveAt(strList.Count - 1);
            //Console.WriteLine("您一共录入{0}个学生,姓王同学的个数为:{1},名字分别为:", strList.Count,count);
            //foreach (string strName in strList)
            //{
            //    Console.WriteLine(strName);
            //}
            //Console.ReadKey();
            #endregion


            #region  将普通日期格式:“2011年6月4日” 转换成汉字日期格式:“二零一一年六月四日”。暂时不考虑10日、13日、23日等“带十”的问题。
            //while(true)
            //{
            //    Console.WriteLine("请输入一个日期:");
            //    string date = Console.ReadLine();
            //    date = ConvertData(date);
            //    Console.WriteLine(date);
            //    Console.ReadKey();    
            //}
            #endregion
        }


        /// <summary>
        /// 将普通日期格式转换成汉字日期格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        private static string ConvertData(string date)
        {
            //不能直接修改字符串,比如data[0]='零',字符串具有不可变性
            //先根据字符串data生成一个char数组(字符数组)
            char[] chs = date.ToCharArray();
            for (int i = 0; i < chs.Length; i++)
            {
                switch(chs[i])
                {
                    case '0':chs[i] = '零';
                        break;
                    case '1':
                        chs[i] = '一';
                        break;
                    case '2':
                        chs[i] = '二';
                        break;
                    case '3':
                        chs[i] = '三';
                        break;
                    case '4':
                        chs[i] = '四';
                        break;
                    case '5':
                        chs[i] = '五';
                        break;
                    case '6':
                        chs[i] = '六';
                        break;
                    case '7':
                        chs[i] = '七';
                        break;
                    case '8':
                        chs[i] = '八';
                        break;
                    case '9':
                        chs[i] = '九';
                        break;
                }
            }
            //return date;
            //将char数组转化为字符串string
            return new string(chs);
        }
    }
}
阅读全文
0 0