c#作业题

                                                                          第三章 语法基础

上机练习

1. 编写一个控制台程序,要求将字符串中的每个字符颠倒输出。

string str = "ABC";
            Console.WriteLine(str);
            for (int i = str.Length - 1; i >= 0; i--)
            {
                Console.Write(str[i]);
            }
            Console.WriteLine();

2. 编写一个控制台程序,要求去掉字符串中的所有空格。

        //2. 编写一个控制台程序,要求去掉字符串中的所有空格。
            string str = "  ABC  Dehg JJ";
            Console.WriteLine(str);
            //替换
            string strss=str.Replace(" ", "");
            Console.WriteLine(strss);
            //分割
            string[] strs = str.Split(new char[] {' '});
            foreach (string item in strs)
            {
                Console.Write(item);
            }
            Console.WriteLine();

3. 编写一个控制台程序,实现从字符串中分离文件路径、文件名及扩展名的功能。

  //3. 编写一个控制台程序,实现从字符串中分离文件路径、文件名及扩展名的功能。
            string path = @"F:c作业3班作业ook.exe";
            string[] paths = path.Split('.');
            Console.WriteLine("扩展名:"+paths[1]);
            string[] pathss=paths[0].Split('\');
            Console.WriteLine(paths[0]);
            Console.WriteLine("文件名:"+pathss[pathss.Length-1]);
            Console.WriteLine("路径:" + paths[0].Remove(8));

4.  输入一个字符,判定它是什么类型的字符(大写字母,小写字母,数字或者其它字符)

//4.  输入一个字符,判定它是什么类型的字符(大写字母,小写字母,数字或者其它字符)
            Console.WriteLine("请输入一个字符:");
            char ch = (char)Console.Read();
            if (char.IsUpper(ch))
            {
                Console.WriteLine("大写字母");
            }
            else if (char.IsLower(ch))
            {
                Console.WriteLine("小写字母");
            }
            else if (char.IsDigit(ch))
            {
                Console.WriteLine("数字");
            }
            else
            {
                Console.WriteLine("其他");
            }

5. 输入一个字符串,将其中小写字母改成大写字母,把大写字母改成小写字母,其余字符不变,输出该字符串。

 

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

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("aaddssFF");
            for (int i = 0; i < sb.Length;i++ )
            {
                if (char.IsUpper(sb[i]))
                {
                    sb.Replace(sb[i], char.ToLower(sb[i]),i,1);
                }
                else if (char.IsLower(sb[i]))
                {
                    sb.Replace(sb[i], char.ToUpper(sb[i]), i, 1);
                }
                     
            }
            Console.WriteLine(sb);
            

        }
    }
}

扩展统计下列字符串中每个单词重复出现的次数(不区分大小写)。

Sample code may fit more than one of these areas. In those cases, place the sample so it matches the topics you are covering in your documents. Ask yourself what readers will learn from reading your topic. What will they learn from building and running your sample?

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

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Sample Sample code may fit more than one of these areas. In those cases, place the sample so it matches the topics you are covering in your documents. Ask yourself what readers will learn from reading your topic. What will they learn from building and running your sample?";
            str = str.ToUpper();
            char[] ch = new char[] { ' ', ',', '.', '?' };
            string[] strs = str.Split(ch);
            Console.WriteLine(strs.Length);
            string[] words = new string[strs.Length];
            int num = 0;
            for (int i = 0; i < strs.Length; i++)
            {
                if (!(words.Contains((strs[i]))))
                {

                    words[num] = strs[i];
                    num++;
                    string temp = strs[i];
                    int count = 0;
                    for (int j = 0; j < strs.Length; j++)
                    {
                        if (strs[j] == temp)
                        {
                            count++;
                        }
                    }
                    Console.WriteLine(temp + "出现了" + count + "次!");
                }

            }
            Console.Read();
        }
    }
}

05 流程控制

1、从键盘输入一个字符,程序检查输入字符是否是小写字符、大写字符或数字。在任何一种情况下,都会显示适当的消息

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

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {
            //1、从键盘输入一个字符,程序检查输入字符是否是小写字符、大写字符或数字
            char ch = Convert.ToChar(Console.ReadLine());
            if (char.IsLower(ch))
            {
                Console.WriteLine("小写字母");
            }
            else if(char.IsUpper(ch))
            {
                Console.WriteLine("大写字母");
            }
            else if(char.IsDigit(ch))
            {
                Console.WriteLine("数字");
            }
        }
    }
}

2、设计一个简单的猜数游戏:随机产生一个1-100的数,要求输入的数与随机产生的数进行比较,如果输入的数大于随机产生的数,提示:“对不起,您猜大了!”;如果输入的数小于随机产生的数,提示:“对不起,您猜小了!”;如果输入的数等于随机产生的数,提示:“恭喜您,您猜对了!”程序结束。

提示:随机产生一个1-100的整数的方法

Random rnd = new Random();//创建随机数种子

int rndNumber = rnd.Next(1, 100);//返回一个指定范围内的整数

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

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {
            //2、设计一个简单的猜数游戏:随机产生一个1-100的数,
            //要求输入的数与随机产生的数进行比较,如果输入的数大于随机产生的数,
            //提示:“对不起,您猜大了!”;如果输入的数小于随机产生的数,提示:“对不起,您猜小了!”
            //;如果输入的数等于随机产生的数,提示:“恭喜您,您猜对了!”程序结束。
            //提示:随机产生一个1-100的整数的方法
            Random rnd = new Random();//创建随机数种子
            int rndNumber = rnd.Next(1, 100);//返回一个指定范围内的整数
            Console.WriteLine("请猜数:");
            while(true)
            {
                int num = Convert.ToInt32(Console.ReadLine());
                if (num < rndNumber)
                {
                    Console.WriteLine("猜小了");
                }
                else if (num > rndNumber)
                {
                    Console.WriteLine("猜大了");
                }
                else
                {
                    Console.WriteLine("恭喜您猜对了!");
                    break;
                }
            }
           
        }
    }
}

3、从键盘输入一个数字作为行数,使用for循环语句,在命令窗口中绘制如图所示的“金字塔”。

 

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

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {       
            //3、从键盘输入一个数字作为行数,使用for循环语句,
            //在命令窗口中绘制如图所示的“金字塔”。
            Console.WriteLine("请输出金字塔的行数:");
            int num = Convert.ToInt32(Console.ReadLine());
            for (int i = num-1; i>=0; i--)
            {
                for (int j = 0; j < i; j++)
                {
                    Console.Write(" ");
                }
                for (int k = 0; k < 2*(num - i)-1; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            
        }
    }
}

4、创建一个控制台程序,计算1+2+3+.....+n!的值并输出,n从键盘输入。

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

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {
            //4、创建一个控制台程序,计算1!+2!+3!+.....+n!的值并输出,n从键盘输入。
            Console.WriteLine("请输入整数n:");
            int n = Convert.ToInt32(Console.ReadLine());
            int temp=1;
            int rel = 0;
            for (int i = n; i > 0; i--)
            {
                temp = 1;
                for (int j = 1; j <= i; j++)
                {
                    temp = temp * j;
                }
                rel = rel + temp;
            }
            Console.WriteLine(rel);
 

            
        }
    }
}

5、创建一个控制台程序,从键盘输入一个作为月份的数字,使用switch语句,将月份所在的季节输出到控制台。

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

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {       
            //5、创建一个控制台程序,从键盘输入一个作为月份的数字,
            //使用switch语句,将月份所在的季节输出到控制台。
            Console.WriteLine("输入月份数字:");
            int mo = Convert.ToInt32(Console.ReadLine());
            switch (mo)
            {
                case 1:
                case 2:
                case 3:
                    Console.WriteLine("春季");
                    break;
                case 5:
                case 6:
                case 4:
                    Console.WriteLine("夏季");
                    break;
                case 7:
                case 8:
                case 9:
                    Console.WriteLine("秋季");
                    break;
                default:
                    Console.WriteLine("冬季");
                    break;
            }
            
        }
    }
}

集合与泛型

1、数制转换问题。数制转换问题是将任意一个非负的十进制数转换为其它进制的数,这是计算机实现计算的基本问题。其一般的解决方法的利用辗转相除法。以将一个十进制数N转换为八进制数为例进行说明。假设N=5142,示例图:

N   N/8(整除)  N%8(求余)      低

5142   642 6

642 80 2                                                                                                                                  

80 10 0

10 1 2

1 0 1 高

从图可知,(5142)10=(12026)8。编写一个控制台程序,实现十进制数转换成八进制数

(提示:转换得到的八进制数各个数位是按从低位到高位的顺序产生的,而转换结果的输出通常是按照从高位到低位的顺序依次输出。也就是说,输出的顺序与产生的顺序正好相反,这与栈的操作原则相符。所以,在转换过程中可以使用一个栈,每得到一位八进制数将其入栈,转换完毕之后再依次出栈。)

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

namespace test3
{

    class Program
    {
        //十进制转八进制
        static void Main(string[] args)
        {
            //以将一个十进制数N转换为八进制数为例进行说明。假设N=5142,示例图:
            //从图可知,(5142)10=(12026)8。编写一个控制台程序,实现十进制数转换成八进制数
            //(提示:转换得到的八进制数各个数位是按从低位到高位的顺序产生的,而转换结果的输出通常是按照从高位到低位的顺序依次输出。也就是说,输出的顺序与产生的顺序正好相反,这与栈的操作原则相符。所以,在转换过程中可以使用一个栈,每得到一位八进制数将其入栈,转换完毕之后再依次出栈。)
            //2、编写一个控制台程序,把控制台输入的数组字符串(如:"123")转换为中文大写(如:壹贰叁)。(要求使用Dictonary<T>)
            //3、编写一个控制台程序,实现List<T>的添加、插入、删除、查找、排序等功能。
            Console.WriteLine("请输入一个十进制的数:");
            int num = Convert.ToInt32(Console.ReadLine());
            Stack<int> nums = new Stack<int>();
            nums.Push(num % 8);
            int  n = num / 8;
            while(n!=0)
            {
                nums.Push(n % 8);
                n = n / 8;
            }
            foreach (int item in nums)
            {
                Console.Write(item);
            }
            Console.ReadKey();        
        }

    }
}

2、编写一个控制台程序,控制台输入的数组字符串(如:"123"转换为中文大写(如:壹贰叁)。(要求使用Dictonary<T>)

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

namespace test3
{

    class Program
    {        

        static void Main(string[] args)
        {
            //2、编写一个控制台程序,把控制台输入的数组字符串(如:"123")转换为中文大写(如:壹贰叁)。
            Console.WriteLine("请输入一个数组字符串:");
            string str = Convert.ToString(Console.ReadLine());
            //(要求使用Dictonary<T>)
            Dictionary<int,string> dic= new Dictionary<int,string>(); 
            dic.Add(1,"");
            dic.Add(2, "");
            dic.Add(3, "");
            dic.Add(4, "");
            dic.Add(5, "");
            dic.Add(6, "");
            dic.Add(7, "");
            dic.Add(8, "");
            dic.Add(9, "");
            dic.Add(0, "");
            Console.WriteLine("转换");
            foreach (char item in str)
            {
                int n = (int)item -48;
                str=str.Replace(Convert.ToString(n),dic[n]);
            }
            Console.WriteLine(str);
            //3、编写一个控制台程序,实现List<T>的添加、插入、删除、查找、排序等功能。
        }
    }
}

3、编写一个控制台程序,实现List<T>的添加、插入、删除查找、排序等功能。

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

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {
            
//3、编写一个控制台程序,实现List<T>的添加、插入、删除、查找、排序等功能。
            //list实例化
            List<string> li = new List<string>();
            //List添加
            li.Add("list1");
            li.Add("list2");
            li.Add("list3");
            li.AddRange(li);
            //list删除
            li.Remove("list1");
            li.RemoveAt(1);
            //list查找
            li.IndexOf("list2");
            //list排序
            li.Sort();
            foreach (string item in li)
            {
                Console.WriteLine(item);
            }
            
        }
    }
}

拓展软件设计大赛题目

文字祖玛游戏需求如下:

1).程序通过控制台输出一个字符串,由ABCDE五个字母组成,例如:ACBEEBBAD

2).用户输入一个字符,只能是ABCDE其中之一,然后再输入一个要插入的位置。

3).程序会将这个字符插入到字符串的指定位置前(第一个字符位置为0,第二个字符位置为1,依此类推),然后消除连续出现的三个相同的字符,直到没有连续三个相同的字符为止。

例如:

控制台输出:ACBEEBBAD

用户输入:E, 3

控制台输出:ACAD

以上示例表示:在位置3插入E后,结果是:ACBEEEBBAD,消除连续的三个E,结果是:ACBBBAD再次消除连续三个B,结果是:ACAD

要求如下:

A.为实现此游戏,需要设计一个方法DealString()

/**

* 参数:

* str: 原始字符串

* index: 要插入字符的位置

* letter: 要插入的字符

* 返回结果: 经过处理后的字符串

**/

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

namespace test3
{

    class Program
    {
        static void Main(string[] args)
        {
            //1).程序通过控制台输出一个字符串,由A、B、C、D、E五个字母组成,例如:ACBEEBBAD。
            Console.WriteLine("请输入一串字符串(ABCDE):");
            string str = Convert.ToString(Console.ReadLine());
            //2).用户输入一个字符,只能是A、B、C、D、E其中之一,然后再输入一个要插入的位置。
            Console.WriteLine("请输入一个字符(ABCDE):");
            string ch = Convert.ToString(Console.ReadLine());
            Console.WriteLine("请输入要插入的位置:");
            int pos = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(str);
            str=str.Insert(1,ch);
            Console.WriteLine(str);
            //3).程序会将这个字符插入到字符串的指定位置前(第一个字符位置为0,第二个字符位置为1,依此类推),然后消除连续出现的三个相同的字符,直到没有连续三个相同的字符为止。
            for (int i = 0; i < str.Length-2; i++)
            {
                if (str[i] == str[i + 1] && str[i + 1] == str[i + 2])
                {                    
                    str=str.Remove(i,1);
                    str=str.Remove(i,1);
                    str=str.Remove(i,1);
                }
            }
            Console.WriteLine(str);
            //例如:
            //控制台输出:ACBEEBBAD
            //用户输入:E, 3
            //控制台输出:ACAD
            //以上示例表示:在位置3插入E后,结果是:ACBEEEBBAD,消除连续的三个E,结果是:ACBBBAD再次消除连续三个B,结果是:ACAD。
            //要求如下:
            //A.为实现此游戏,需要设计一个方法DealString()
            /**
            * 参数:
            * str: 原始字符串
            * index: 要插入字符的位置
            * letter: 要插入的字符
            * 返回结果: 经过处理后的字符串
            **/
        
        }

    }
}

 

11 属性和索引器

上机练习题

1、编写代码实现需求: 

编写一个类Student,代表学员,要求: 

(1)具有属性:姓名、年龄,其中年龄不能小于16岁,否则输出错误信息 

(2)具有方法:自我介绍,负责输出该学员的姓名、年龄

2. 编写一个类Student1,代表学员,要求: 

(1)具有属性:姓名、年龄、性别、专业 

(2)具有方法:自我介绍,负责输出该学员的姓名、年龄、性别、以及专业 

(3)具有两个带参构造方法:第一个构造方法中,设置学员的性别为男、专业为计算机,其余属性的值由参数给定;第二个构造方法中,所有属性的值都由参数给定

(4)编写测试类

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

namespace test3
{
//编写一个类Student,代表学员,要求: 
    //(1)具有属性:姓名、年龄,其中年龄不能小于16岁,否则输出错误信息 
    //(2)具有方法:自我介绍,负责输出该学员的姓名、年龄
    class student
    {
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        int age;

        public int Age
        {
            get { return age; }
            set {
                if (value >= 16)
                {
                    age = value;
                }
                else
                {

                    Console.WriteLine("error");
                }
            }
        }
        public void intro()
        {
            Console.WriteLine("姓名:"+name);
            Console.WriteLine("年龄:"+age);

        }
        public student(string name, int age)
        {
            this.age = age;
            this.name = name;
        }


    }
//2. 编写一个类Student1,代表学员,要求: 
//    (1)具有属性:姓名、年龄、性别、专业 
//(2)具有方法:自我介绍,负责输出该学员的姓名、年龄、性别、以及专业 
    class student1
    {
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        string sex;

        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        string zhuanye;

        public string Zhuanye
        {
            get { return zhuanye; }
            set { zhuanye = value; }
        }
        public void intro()
        {
            Console.WriteLine("我的姓名是:"+name);
            Console.WriteLine("我的年龄是:"+age);
            Console.WriteLine("我得性别是:"+sex);
            Console.WriteLine("我的专业是:"+zhuanye);
           

        }
         public student1(string name,int age,string sex,string zhuanye)
         {
             this.name=name;
             this.age=age;
             this.sex=sex;
             this.zhuanye=zhuanye;

         }
         public student1(string name, int age)
         {
             this.name = name;
             this.age = age;
             this.sex = "nan";
             this.zhuanye = "it";

         }
    
    }
//(4)编写测试类
//(3)具有两个带参构造方法:第一个构造方法中,
//    设置学员的性别为男、专业为计算机,其余属性的值由参数给定;第二个构造方法中,所有属性的值都由参数给定
    class Program
    {
        static void Main(string[] args)
        {
            student stu1 = new student("name2",14);
            student1 stu2 = new student1("name3",17,"nv","it");
            student1 stu3 = new student1("name4",15);
            stu1.intro();
            Console.WriteLine();
            stu2.intro();
            Console.WriteLine();
            stu3.intro();

        }

    }
}

3、属性和索引练习

(1) 定义一个Student类,其包含属性:SId(学号), Name(姓名),Score(总分),并重载其构造方法,初始化其属性。

(2) 定义一个班级类ClassDemo,其包含属性Count(学生人数,只读),List<Student>(学生列表);定义其索引器,按照序号获得学生对象,按照学号和姓名获得学生对象(索引重载)。

(3) 编写测试类,创建4个学生对象:

学号

姓名

总分

1

张三

375

2

李四

400

3

王五

425

4

薛六

498

  1. 将以上学生添加到班级,输出班级人数,并遍历班级学生的信息;
  2. 使用索引器查找学号是2,姓名是“李四”的总分,并输出到控制台。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test3
{
    class student
    {
        string sid;

        public string Sid
        {
            get { return sid; }
            set { sid = value; }
        }

        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        int score;

        public int Score
        {
            get { return score; }
            set { score = value; }
        }
        public student(string sid, string name, int score)
        {
            this.sid = sid;
            this.name = name;
            this.score = score;
        }
    }

    class ClassDemo
    {
        int count;

        public int Count
        {
            get
            {
                count = stu.Count;
                return count;
            }
            set { count = value; }
        }
        List<student> stu;

        internal List<student> Stu
        {
            get { return stu; }
            set { stu = value; }
        }
        //定义其索引器,按照学号和姓名获得学生对象(索引重载)。
        public student this[int index]
        {
            get
            {
                if (index > 0 && index < stu.Count)
                {
                    return stu[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (index >= 0 && index < stu.Count)
                {
                    stu[index] = value;
                }
            }
        }

        public student this[string sid]
        {
            get
            {
                for (int i = 0; i < stu.Count; i++)
                {
                    if (stu[i].Sid == sid)
                    {
                        return stu[i];
                    }
                }
                return null;
            }
            set
            {
                for (int i = 0; i < stu.Count; i++)
                {
                    if (stu[i].Sid == sid)
                    {
                        stu[i] = value;
                    }
                }
            }
        }
        public student this[string sid, string name]
        {
            get
            {
                foreach (student item in stu)
                {
                    if (item.Sid == sid && item.Name == name)
                    {
                        return item;
                    }
                }
                return null;
            }
            set
            {
                for (int i = 0; i < stu.Count; i++)
                {
                    if (stu[i].Name == name && stu[i].Sid == sid)
                    {
                        stu[i] = value;
                    }
                }
            }
        }
        public ClassDemo(int count)
        {
            this.count = count;
            stu = new List<student>(count);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //(3) 编写测试类,创建4个学生对象:
            student stu1 = new student("001", "name1", 98);
            student stu2 = new student("002", "name2 ", 94);
            student stu3 = new student("003", "name3", 95);
            student stu4 = new student("004", "name4", 92);
            //将以上学生添加到班级,输出班级人数,并遍历班级学生的信息;
            ClassDemo cd = new ClassDemo(4);
            cd.Stu.Add(stu2);
            cd.Stu.Add(stu2);
            Console.WriteLine();
            cd.Stu.Add(stu3);
            cd.Stu.Add(stu4);
            foreach (student item in cd.Stu)
            {
                Console.Write(item.Name + " ");
            }
            Console.WriteLine();
            cd[0] = stu2;
            cd[1] = stu2;
            cd[2] = stu2;
            cd[3] = stu4;
            foreach (student item in cd.Stu)
            {
                Console.Write(item.Name + "* ");
            }
            Console.WriteLine();
            Console.WriteLine(cd.Count);
            //使用索引器查找学号是004,姓名是“name4”的总分,并输出到控制台。
            Console.WriteLine(cd["004", "name4"].Score);
        }

    }
}

13.抽象类和接口

上机练习题

1、定义一个Shape类,该类有名为ColorString类型的数据成员,还有一个名为GetColor方法(用于获取图形的颜色),另外还有一个名为GetArea的抽象方法。在Shape类的基础上定义其子类Circle,子类有半径R数据成员,并提供对GetArea方法的实现。以计算相应图形的面积。

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

namespace test3
{
    //1、定义一个Shape类,该类有名为Color的String类型的数据成员,
    class shape
    {
        string color;
        public string Color
        {
            get { return color; }
            set { color = value; }
        }
        //还有一个名为GetColor方法(用于获取图形的颜色),
        public string Getcolor()
        {
            return color;
        }
        //另外还有一个名为GetArea的抽象方法。
        public virtual double  GetArea()
        {
            return 0.0;
        }
        public  shape(string color)
        {
            this.color = color;
        }
    }
    //在Shape类的基础上定义其子类Circle,
    //子类有半径R数据成员,并提供对GetArea方法的实现。
    //以计算相应图形的面积。
    class Circle:shape
    {
        int r;
        public override double GetArea()
        {
            return r * Math.PI * Math.PI;
        }
        public Circle(string color,int r):base(color)
        {
            this.r=r;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            shape sh = new Circle("red",1);
            Console.WriteLine("该圆的面积为"+sh.GetArea());
            Console.WriteLine("该圆的颜色是:"+sh.Getcolor());
        }
    }
}

2、定义如下接口,任何类使用该接口都可产生一系列数字。

public interface IDataSeries
{
    int  GetNext();    //返回下一个系列数字
    void Reset();      //重新开始
    void SetInit(int i);  //设置系列数字的开始值
}

创建一个类FiveSeries,实现上述接口,用于产生一个公差为5的的数列。

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

namespace test3
{
    public interface IDataSeries
    {
        int GetNext();    //返回下一个系列数字
        void Reset();      //重新开始
        void SetInit(int i);  //设置系列数字的开始值
    }
    public class get
    {
        int num;
        int temp;
        public int GetNext()
        {
            num = num + 5;
                return num;
        }
        public void Reset()
        {
            this.num = temp;
        }
        public void SetInit(int i)
        {
            this.num = i-5;
            this.temp = i;
        }
        
    }
    class Program
    {
        static void Main(string[] args)
        {
            get get = new get();
            get.SetInit(1);
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(get.GetNext());
            }
      }
                
    }
}

15.文件操作

上机练习题

创建一个控制台程序,功能要求:

1、创建一个名为Letter的文本文件;

2、把A-Z的26个英文字母保存到上述文件中;

3、把Letter文件中的内容读取到程序中;

4、把Letter文件名修改为New-Letter;

5、拷贝New-Letter文件到Copy-Letter;

6、删除New-Letter和Copy-Letter文件。

原文地址:https://www.cnblogs.com/excellencesy/p/8320934.html