C#基础篇十小练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace P03练习
{
    class Program
    {
        static void Main(string[] args)
        {
            Test05();
        }
 
        public static void Test01()
        {
            /*请编写1个方法,求一列数的规则如下: 1、1、2、3、5、8、13、21、34、55......  求指定位数的值是多少.并返回这个数
             * (前2位为1,后面每1位为前2位的和.)
             */
            int res = Test01_1(6);
            Console.WriteLine(res);
        }
 
        public static int Test01_1(int plugNum)
        {
            //1.如果输入的位数是 <=2的,直接返回 1
            if (plugNum <= 2) return 1;
 
            //2.如果大于2的话,则进行运算
            int prev1 = 1;
            int prev2 = 1;
            int res = 0;
 
            for (int i = 1; i <= plugNum - 2; i++)
            {
                res = prev1 + prev2;
                prev1 = prev2;
                prev2 = res;
            }
 
            return res;
        }
 
        public static void Test02()
        {
            /* 2.写1个方法,接收圆的半径. 将圆的面积和周长返回. π取值3.14 圆的面积=π*半径的平方、圆的周长=π* 2*半径(10分) */
            float[] arrResult = new float[2];//下标为0 代表 面积;下标为 1 代表 周长
            Test02_01(5, arrResult);
            Console.WriteLine("半径为{0}圆圈的 面积是{1},周长是{2}", 5, arrResult[0], arrResult[1]);
        }
 
        /// <summary>
        /// 计算 周长 和 面积
        /// </summary>
        /// <returns></returns>
        public static void Test02_01(int half, float [] arr)
        {
            arr[0] = 3.14f * half * half;
            arr[1] = 2 * 3.14f * half;
        }
 
 
        public static void Test03_01()
        {
            /* 请编写1个程序,该程序从控制台接收用户的输入班级的人数,然后分别从控制台接收每1个人的成绩.只要有1个的成绩不合法(不在0-100的范围或者输入的不是整数),就提示用户重新输入该名学生的成绩.当所有的学生的成绩输入完毕之后,请打印出全班平均分,然后再求出去掉1个最高分和去掉1个最低分后的平均分,然后将成绩由高到低的顺序打印出来.(25分)
             */
            Console.WriteLine("接收学员的个数:");
            int max = 0;//最高分
            int min = 0;//最低分
            int scoreTotal = 0;//总分数
            int count = int.Parse(Console.ReadLine());//总人数
            int[] arrScore = new int[count];//分数数组
 
            for (int i = 0; i < count; i++)
            {
                int score = Test03_01_01("请输入第" + (i + 1) + "学员的分数:");
                arrScore[i] = score;
                if (i == 0)
                {
                    max = score;
                    min = score;
                }
                scoreTotal += score;//加入总分数
                if (max < score) max = score;//判断是否为本次循环之内的最高分
                if (min > score) min = score;//判断是否为本次循环之内的最低分
            }
 
            float avg0 = (scoreTotal - max - min) * 1.0f / (count - 2);
 
            //1.0 排序
            List<int> list = arrScore.ToList();
            list.Sort();
 
            Console.WriteLine("总人数为{0},全班平均分为{1},去掉最高分后最低分后的平均分为{2}",
                count,
                scoreTotal * 1.0f / count,
                avg0);
 
            Console.WriteLine("由低到高显示分数:");
            foreach (int i in list)
            {
                Console.WriteLine(i);
            }
        }
        //提示用户输入一个 整型的数值,并且 必须在1-100之间
        public static int Test03_01_01(string strMsg)
        {
            int res = -1;
            while (true)
            {
                Console.WriteLine(strMsg);
                string str = Console.ReadLine();
                if (int.TryParse(str, out res))//将用户输入的字符串转成 数值
                {
                    if (res >= 0 && res <= 100)//判断数值知否为 0-100之间的数
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("必须是0-100之间的数值!");
                    }
                }
                else
                {
                    Console.WriteLine("必须输入数值~~!");
                }
            }
            return res;
        }
 
        public static void Test04()
        {
            /* 编一个程序,输入用户名和密码,实现用户登录程序的功能,至多允许输入三次,超过三次不允许登录并退出程序 */
            string okName = "";
            string okPwd = "";
            int i = 0;
            while (true)
            {
                Console.WriteLine("请输入您的用户名:");
                okName = Console.ReadLine();
                Console.WriteLine("请输入您的密码:");
                okPwd = Console.ReadLine();
                if (okName == "james" && okPwd == "123")
                {
                    Console.WriteLine("用户登录成功了~~~!");
                    break;
                }
                else
                {
                    Console.WriteLine("用户名或密码输入错误~~!");
                    i++;
                    if (i >= 3)
                    {
                        Console.WriteLine("对不起,您的输入次数超过3次程序退出~~");
                        Console.ReadLine();
                        break;
                    }
                }  
            }
        }
 
        public static void Test05()
        {
            /*
             * 有1个整型的数组,假设每1个元素的值都是1个正整数(意思就是每1个元素的值都是大于0的.)
             * 请编写1个方法,该方法将数组中重复的元素去掉,并返回1个没有重复数据的数组.
             * 比如:有数组A={1,2,3,3,4,4,5,6,6},方法返回1个新数组B={1,2,3,4,5,6}
             */
 
            int[] arr = { 1, 2, 100, 99, 99, 88, 77, 3, 3, 4, 4, 5, 6, 6, 99 };
            int[] arrNew = GetANewArr(arr);
        }
 
        public static int[] GetANewArr(int[] arr)
        {
            int[] arrTemp = new int[arr.Length];
            int indexNum = 0;//记录不重复数值的数量
 
            //1.循环 找出  不重复的 数值,存入临时数组中
            for (int i = 0; i < arr.Length; i++)
            {
                int num = arr[i];//取出 一个  源数组里的 数值
                bool isExist = false;//记录本轮数字 是否在 临时数组中存在
 
                for (int j = 0; j < arrTemp.Length; j++)//用 源数组里的 数值,去 临时数组中比较,如果没有一样的,则标记一下
                {
                    if (arrTemp[j] == num)
                    {
                        isExist = true;
                    }
                }
                if (!isExist)//如果 原数组数值 在 临时数组中不存在,则 保存到 临时数组中
                {
                    arrTemp[indexNum] = arr[i];
                    indexNum++;//自增 不重复数值的 个数
                }
            }
            //2.按照找出的不重复数值的个数,创建新数组
            int [] arrNew = new int[indexNum];
            //2.1将 不重复的数值 从 临时数组中 复制到 新数组中
            for (int i = 0; i < indexNum; i++)
            {
                arrNew[i] = arrTemp[i];
            }
            //2.2返回新数组
            return arrNew;
        }
 
    }
}

  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace P04练习
{
    class Program
    {
        static void Main(string[] args)
        {
 
            Console.ReadLine();
        }
 
        public static void No1()
        {
            /*
             * 定义1个汽车类,有品牌 轮子数量 颜色 价格 型号 座位数量 字段,
             * 有1个行驶的方法,
             * 还有1个停止的方法
             * 还有1个自己介绍自己的方法(显示自己的品牌 轮子 颜色 价格 型号 座位。。信息.);
             * 创建对象并测试.
             */
 
            QiChe qicheNo1 = new QiChe();
            qicheNo1.XingShi();
            qicheNo1.TingZhi();
        }
 
        public static void No2()
        {
            Student stu = new Student();
            stu.Name = "";
            /*
             * 定义1个学生类,有姓名、年龄、手机号码、学号四个属性,
                要求学生的姓名不能为空串,并且长度不能小于2,否则使其默认值为”无名”,
                年龄只能在0-100之间,否则默认值为18,
                手机号码只能为11位的数字。否则就给默认值13444444444
                学号要求为10个数字,否则赋值为默认值”0000000000”.
                再定义1个方法介绍自己,将自己的姓名年龄手机号码打印出来。
 
                然后再定义1个教师类,教师类也有姓名年龄手机号码三个属性要求和学生类一样,
                但是教师类还有1个属性工资 要求值再1000-2000之间,否则默认值为1000,、
                教师也有介绍自己自己的方法(显示自己的所有信息 姓名 年龄 性别 工资)
             */
        }
    }
}

  汽车类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace P04练习
{
    /// <summary>
    /// 汽车
    /// </summary>
    public class QiChe
    {
        private string pinPai;
        private int lunZiShuLiang;
        private string yanSe;
 
        #region 1.0 行驶方法 +void XingShi()
        /// <summary>
        /// 行驶方法
        /// </summary>
        public void XingShi()
        {
            Console.WriteLine("汽车在行驶~~~");
        }
        #endregion
 
        #region 2.0 停止方法 刹车 +void TingZhi()
        /// <summary>
        /// 停止方法 刹车
        /// </summary>
        public void TingZhi()
        {
            Console.WriteLine("汽车在停住了~~~");
        }
        #endregion
 
        #region 3.0 介绍自己  void Show()
        /// <summary>
        /// 3.0 介绍自己
        /// </summary>
        public void Show()
        {
 
        }
        #endregion
    }
}

  学生类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace P04练习
{
    /// <summary>
    /// 学员类
    /// </summary>
    public class Student
    {
        private int id;
        private string name;
        private int age;
        private int cellphone;
 
        #region 1.0 姓名 +string Name
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get { return name; }
            set
            {
                if (string.IsNullOrEmpty(value) || value.Length < 2)
                {
                    name = "无名";
                }
                else
                {
                    name = value;
                }
            }
        }
        #endregion
 
 
    }
}

  

计算器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace P05计算器
{
    public enum CulType
    {
        Jia = 1,
        Jian = 2,
        Chen = 3,
        Chu = 4
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace P05计算器
{
    /// <summary>
    /// 计算器
    /// </summary>
    public class JiSuanQi
    {
        #region 1.0 计算 +void JiSuan()
        /// <summary>
        /// 1.0 计算
        /// </summary>
        public void JiSuan()
        {
            //1.要求用户输入 运算符号 +,-,*,/
            Console.Write("请输入运算符(1+,2-,3*,4/):");
            int curType = int.Parse(Console.ReadLine());
 
            //2.接收 第一个数值
            Console.WriteLine("请输入第一个数值:");
            int numA = int.Parse(Console.ReadLine());
            //3.接收 第二个数值
            Console.WriteLine("请输入第二个数值:");
            int numB = int.Parse(Console.ReadLine());
            //运算结果
            int res = -1;
 
            //4.根据操作符进行运算
            CulType type = (CulType)curType;
            #region 根据操作符进行运算
            switch (type)
            {
                case CulType.Jia:
                    {
                        res = numA + numB;
                        break;
                    }
                case CulType.Jian:
                    {
                        res = numA - numB;
                        break;
                    }
                case CulType.Chen:
                    {
                        res = numA * numB;
                        break;
                    }
                case CulType.Chu:
                    {
                        res = numA / numB;
                        break;
                    }
            }
            #endregion
 
            Console.WriteLine("{0} {1} {2}={3}", numA, GetOpeName(type), numB, res);
        }
        #endregion
 
        #region 1.1 根据枚举返回对应的 运算符号 -string GetOpeName(CulType type)
        /// <summary>
        /// 1.1 根据枚举返回对应的 运算符号
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        string GetOpeName(CulType type)
        {
            string name = "";
            switch (type)
            {
                case CulType.Jia:
                    {
                        name = "+";
                        break;
                    }
                case CulType.Jian:
                    {
                        name = "-";
                        break;
                    }
                case CulType.Chen:
                    {
                        name = "*";
                        break;
                    }
                case CulType.Chu:
                    {
                        name = "/";
                        break;
                    }
            }
            return name;
        }
        #endregion
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace P05计算器
{
    class Program
    {
        static void Main(string[] args)
        {
            JiSuanQi jsq = new JiSuanQi();
            jsq.JiSuan();
            Console.ReadLine();
        }
    }
}

  

 

 

 

 

 

 

 

 

 

posted @   枫伶忆  阅读(1023)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示