码迷,mamicode.com
首页 > Windows程序 > 详细

传智的光辉岁月-C#基础篇十小练习

时间:2015-01-17 20:51:20      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

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;
        }

    }
}

  

 

 

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,、
                教师也有介绍自己自己的方法(显示自己的所有信息 姓名 年龄 性别 工资)
             */
        }
    }
}

  汽车类

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
    }
}

  学生类

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


    }
}

  

计算器

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
    }
}

  

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
    }
}

  

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();
        }
    }
}

  

技术分享

 

技术分享

 

技术分享

 

技术分享

 

 

 

技术分享

 

技术分享

 

技术分享

 

传智的光辉岁月-C#基础篇十小练习

标签:

原文地址:http://www.cnblogs.com/fenglingyi/p/4230945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!