C#基礎練習題

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

namespace 練習
{
    class Program
    {

        #region 利用方法來說明方法的重載
        static void M()
        {
            
        }
        static void M(int A)
        {

        }
        static void M(string A)
        {

        }
        static void M(int A,int B)
        {

        }
        static void M(int A,string B)
        {

        }
        static void M(string A,int B)
        {

        }
        #endregion
        static void Main(string[] args)
        {
            #region 求一個數組的平均值{1,3,5,7,90,2,4,6,8,10} 有小數則顯示小數點後兩位
            //int[] arrInt = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 ,1,3,5};
            //double avg = GetAvg(arrInt);
            //Console.WriteLine("平均值是:{0}",avg);
            //Console.ReadLine();
            #endregion

            #region 通過冒泡排序對整數數組{1,3,5,7,90,2,4,6,8,10}實現升序排序
            //int[] arrInt={ 1,3,5,7,90,2,4,6,8,10 };
            //for (int i = 0; i < arrInt.Length; i++)
            //{
            //    for (int j = arrInt.Length-1; j > i; j--)
            //    {
            //        if (arrInt[j] < arrInt[j - 1])
            //        {
            //            int num = arrInt[j];
            //            arrInt[j] = arrInt[j - 1];
            //            arrInt[j - 1] = num;
            //        }

            //    }
            //}
            //for (int n = 0; n < arrInt.Length; n++)
            //{
            //    Console.WriteLine(arrInt[n]);
            //}
            //Console.ReadLine();
            #endregion
            #region 有如下字符串:【"患者:"大夫,我咳嗽得很重。" 大夫:"你多大年記?" 患者:"七十五歲。" 大夫:"二十歲咳嗽嗎"患者:"不咳嗽。" 大夫:"四十歲時咳嗽嗎?" 患者:"也不咳嗽。" 大夫:"那現在不咳嗽,還要等到什麼時咳嗽?""】。需求:①請統計出該字符中"咳嗽"二字的出現次數,以及每次"咳嗽"出現的索引位置。②擴展(*):統計出每個字符的出現次數(後面)
            //string msg= "患者:大夫,我咳嗽得很重。 大夫:你多大年記? 患者:七十五歲。 大夫: 二十歲咳嗽嗎 患者: 不咳嗽。 大夫:四十歲時咳嗽嗎? 患者:也不咳嗽。大夫:那現在不咳嗽,還要等到什麼時咳嗽?";
            ////Indexof()  返回指定字符第一次出現的索引位置
            ////LastIndexOf()  返回指定字符最後一次出現的索引位置

            ////記錄咳嗽出現的次數
            //int count = 0;
            ////出現的索引位置
            //int index = 0;
            //while ((index=msg.IndexOf("咳嗽",index))!=-1)//條件就是查找後返回不是-1
            //{
            //    count++;
            //    Console.WriteLine("第{0}次出現【咳嗽】的位置是:{1}",count,index);
            //    index += "咳嗽".Length;
            //}
            //Console.WriteLine("咳嗽總共出現了{0}次!",count);
            //Console.ReadKey();
            //===========================================================
            //通過集合統計字符出現的次數
            //Dictionary<char, int> dict = new Dictionary<char, int>();
            //for (int i = 0; i < msg.Length; i++)
            //{
            //    if (!dict.ContainsKey(msg[i]))
            //    {
            //        dict.Add(msg[i],1);
            //    }
            //    else
            //    {
            //        dict[msg[i]]++;
            //    }             
            //}
            //foreach (KeyValuePair<char, int> item in dict)
            //{
            //    Console.WriteLine("字符{0}出現了{1}次!", item.Key, item.Value);
            //}
            //Console.ReadKey();
            #endregion

            #region 移除字符串空格並以單個空格相連
            //string msg = "  hello          world,你   好   世界  !";
            //msg= msg.Trim();//移除空白
            //string[] vachar= msg.Split(new char[] {' '},StringSplitOptions.RemoveEmptyEntries);
            //msg=string.Join(" ", vachar);
            //Console.WriteLine("================"+msg+"=============");
            //Console.ReadKey();
            #endregion

            #region 要求提示用戶持續錄入學生姓名,當用戶輸入quit(不區分大小寫)時,程序停止接收用戶輸入,並且顯示輸入的學生個數,以及每個學生的姓名
            //string name = string.Empty;
            //List<string> list = new List<string>();

            //do
            //{
            //    Console.WriteLine("請輸入學生姓名:");
            //    name = Console.ReadLine();
            //    list.Add(name);
            //} while (name.ToLower()!="quit");
            //list.RemoveAt(list.Count - 1);
            //Console.WriteLine("共有學生{0}名",list.Count);
            //Console.WriteLine("分別是:");
            //for (int i = 0; i < list.Count; i++)
            //{
            //    Console.WriteLine(list[i]);
            //}
            //Console.ReadKey();
            //=============顯示姓李的學生個數============
            /*string name = string.Empty;
            int count = 0;//姓李的學生個數
            List<string> list = new List<string>();
            do
            {
                Console.WriteLine("請輸入學生姓名:");
                name = Console.ReadLine();
                list.Add(name);
                if (name.IndexOf("李")==0)
                {
                    count++;
                }

            } while (name.ToLower()!="quit");
            list.RemoveAt(list.Count-1);
            Console.WriteLine("共有學生{0}名",list.Count);
            Console.WriteLine("分別是:");
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
            Console.WriteLine("姓李的學生個數爲:{0}",count);
            Console.ReadKey();*/
            #endregion

            #region 將普通日期轉換爲中文格式日期 不考慮帶十的問題
            /*  string msg = "2020年4月16日";
              msg = conwertToDate(msg);

              Console.WriteLine(msg);
              Console.ReadKey();*/

            #endregion

            #region 創建一個類:Person類,屬性:姓名、性別、年齡;方法:SayHi().在創建一個類Proece來繼承Person,並且添加一個屬性Salary重寫SayHi方法。
            #endregion
            #region 編寫一個類:ItcastCalss,該類中有一個私有字段_names,數據類型爲:字符串數組,長度爲5個默認的姓名。要求:爲ItcastClass類編寫一個索引器,要求該索引器能夠通過下標來訪問_names中的內容。

            //ItcastClass ic = new ItcastClass();

            //for (int i = 0; i < ic.Count; i++)
            //{
            //    Console.WriteLine(ic[i]);
            //}
            //Console.WriteLine(ic[3]); 
            //Console.ReadKey();
            #endregion
            //M();//六個方法重載

             #region  請將字符串數組{ "石中玉", "石破天", "叮叮噹噹", "阿秀", "白自在" }中的內容反轉後在輸出,不得使用Reverse()方法。
            //反轉次數爲字符串長度取整  數組都是引用類型
            //string[] msg = { "石中玉", "石破天", "叮叮噹噹", "阿秀", "白自在" };
            //MyReverse(msg);
            //for (int i = 0; i < msg.Length; i++)
            //{
            //    Console.WriteLine(msg[i]);
            //}
            //Console.ReadKey();
            #endregion
        }
        /// <summary>
        /// 反轉字符串方法
        /// </summary>
        /// <param name="msg"></param>
        private static void MyReverse(string[] msg)
        {
            for (int i = 0; i < msg.Length/2; i++)
            {
                string tmp = msg[i];
                msg[i] = msg[msg.Length - 1 - i];
                msg[msg.Length - 1 - i] = tmp;
            }
        }

        /// <summary>
        /// 轉換日期爲中文格式日期
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        private static string conwertToDate(string msg)
        {
            char[] chas=msg.ToCharArray();
            #region  循環該變字符
            for (int i = 0; i < chas.Length; i++)
            {
                switch (chas[i])
                {
                    case '0':
                        chas[i] = '零';
                        break;
                    case '1':
                        chas[i] = '一';
                        break;
                    case '2':
                        chas[i] = '二';
                        break;
                    case '3':
                        chas[i] = '三';
                        break;
                    case '4':
                        chas[i] = '四';
                        break;
                    case '5':
                        chas[i] = '五';
                        break;
                    case '6':
                        chas[i] = '六';
                        break;
                    case '7':
                        chas[i] = '七';
                        break;
                    case '8':
                        chas[i] = '八';
                        break;
                    case '9':
                        chas[i] = '九';
                        break;
                    default:
                        break;
                }
            }
            #endregion

            return new  string(chas);
        }

        /// <summary>
        /// 數組求平均值
        /// </summary>
        /// <param name="arrInt"></param>
        /// <returns></returns>
        private static double GetAvg(int[] arrInt)
        {
            int num = 0;
            for (int i = 0; i < arrInt.Length; i++)
            {
                num += arrInt[i];

            }
            return Math.Round( num / (double)arrInt.Length,2);
        }
    }
}

解決方案

Person類

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

namespace 練習
{
    public class Person
    {
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }

        }

        public int Age
        {
            get;
            set;
        }

        private bool _gender;
        public bool Gender 
        {
            get
            {
                return _gender;
            }
            set
            {
                _gender = value;
            }

        }

        public virtual void SyaHi()
        {
            Console.WriteLine("Hi、、、、、");
        }
    }
}

Proece類

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

namespace 練習
{
    class Proece:Person
    {
        private double _salary;
        public double Salary
        {
            get
            {
                return _salary;
            }
            set
            {
                _salary = value;
            }                
        }
        public override void SyaHi()
        {
            Console.WriteLine("New Hi、、、、、、");
        }
    }
}

ItcastClass類

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

namespace 練習
{
    public class ItcastClass
    {
        /// <summary>
        /// Count屬性來記錄字段長度
        /// </summary>      
        public int Count { get { return _names.Length; } }

        private string[] _names = { "石中玉", "石破天", "叮叮噹噹", "阿秀", "白自在" };

        //索引器  與屬性相同特殊的屬性 編譯成Item的屬性
        public string this[int index]
        {
            get 
            {
                if (index<0||index>=_names.Length)
                {
                    throw new Exception();
                }
                return _names[index];
            }
            set 
            {
                _names[index] = value;
            }
        }

       //索引器還可以重載
        public string this[string username]
        {
            get
            {
                return "";
            }
            set
            {
               
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章