当前位置:网站首页>C exercise. Class list plus records, display records and clear records

C exercise. Class list plus records, display records and clear records

2022-06-26 19:27:00 laocooon

using System;
namespace  Enemy list 
{
    internal class Program
    {
        static void Main(string[] args)
        {
            NameList nList = new NameList();
            nList.Add(" Cold ice ");
            nList.Add(" galen ");
            nList.Add(" spider ");
            nList.List();// Show 
            nList.Add(" faker ");
            nList.Add(" The bomb people ");
            nList.Add(" The tree ");
            nList.Add(" Female gun ");
            nList.Add(" The goddess of war ");
            nList.Add(" Swift scout ");
            nList.Add(" Chief Tauren ");
            nList.List();// Show 
            nList.Add(" Night hunter ");
            nList.Add(" Don't put out the thunder ");
            nList.Add(" The shadow of the blade ");
            nList.List();// Show 

            nList.Clear();// Empty 
            nList.List();// Show 

            Console.ReadKey();
        }
    }

    class NameList
    {
        private const int MAX = 10;// Maximum length 
        private string[] Name=new string[10];
        private int cnt;// Actual length 
        public NameList()
        {
            cnt = 0;
        }
        public void Add(string name)// Increase personnel 
        {
            
            if (cnt == MAX)// Represents the full condition 
            {
                // The data moves to the left as a whole 
                for(int i=0;i<MAX-1;i++)
                {
                    Name[i] = Name[i+1];
                }
                Name[MAX - 1] = name;                
            }
            else// Dissatisfaction 
            { 
                Name[cnt] = name;                
                cnt++;
            }
            Console.WriteLine(" Add personnel information :" + name);
        }
        public void List()// Show all people 
        {
            Console.WriteLine(" The number of people whose names are displayed is :"+cnt.ToString());

            for (int i = 0; i < cnt; i++)
            { 
                Console.WriteLine(Name[i]);
            }
        }
        public void Clear()// Empty all personnel 
        {
            Console.WriteLine(" Cleared the list :");
            for (int i = 0; i < cnt; i++)
            {
                Name[i] = "";
            }
            cnt = 0;
        }
    }

}

原网站

版权声明
本文为[laocooon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261911120667.html