.net(三个简单的作业)

来源:互联网 发布:网络霸凌 社交平台 编辑:程序博客网 时间:2024/05/22 01:30

1,读入一个小数,将小数部分和整数部分交换:例如 3.14 ---〉14.3

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TestExchange{    class Program    {        static void Main(string[] args)        {            while (true)            {                // 1,从控制台读入一个小数,存放在 testDouble 中                string testDouble = Console.ReadLine();                // 2,转为double 类型                double result;                // 用try ... catch ... 语句捕获格式错误的异常                try                {                    result = double.Parse(testDouble);                    // 3,将testDouble以‘.’隔开                    string[] splitStr = testDouble.Split('.');                    // 4,格式输出                    Console.WriteLine("{0}.{1}", int.Parse(splitStr[1]), int.Parse(splitStr[0]));                }                catch (FormatException e)                {                    // 捕获数字格式不正确的异常                    Console.WriteLine("输入数字格式不正确");                }                catch (System.IndexOutOfRangeException e)                {                    // 捕获全部整数不包含小数点的异常                    Console.WriteLine("输入的数应包含小数点");                }            }        }    }}


 

2,定义一个qq状态的枚举(在线、隐身、离线)。

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TestQQState{    class Program    {        static void Main(string[] args)        {        }    }    // 定义QQ状态的枚举    enum QQState    {        OnLine,     //在线        OffLine,    //离线        Hidding,    //隐身        Busy        //忙碌    }}


3, 老婆给当程序员的老公打电话:“下班顺路买10个包子,如果看到卖西瓜的,买一个。”下班后,老公买回一个包子,因为他看到卖西瓜的了,老婆气晕了。分别用程序表达这句话的意思:老婆的意思;程序员老公理解的意思。

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TestJok{    class Program    {        static void Main(string[] args)        {            Husband husband = new Husband();            // 买包子的个数            int bzCount;            // 买西瓜的个数            int xgCount;            // 是不是有买西瓜的            bool isThereHasXiGua = false;            // 老婆的意思            husband.getOffWork();       // 老公下班            husband.buyBaozi(10);       // 老公买十个包子            if (isThereHasXiGua == true) // 如果看到卖西瓜的            {                husband.buyXiGua(1);    // 买一个西瓜            }                        //老公的意思            husband.getOffWork();           // 老公下班            if (isThereHasXiGua == false)    // 如果没有看到买西瓜的            {                husband.buyBaozi(10);       // 买 10 个包子            }            else                            // 如果看到卖西瓜的            {                husband.buyBaozi(1);        // 买 1 个包子            }        }    }    /// <summary>    /// Husband 类    /// </summary>    class Husband    {        /// <summary>        /// 买包子的方法        /// </summary>        /// <param name="bzCount">买的包子的个数</param>        public void buyBaozi(int bzCount)        {        }        /// <summary>        /// 下班的方法        /// </summary>        public void getOffWork()         {         }        /// <summary>        /// 买西瓜的方法        /// </summary>        /// <param name="xgCount">买的西瓜的个数</param>        public void buyXiGua(int xgCount)        {        }    }}


 

原创粉丝点击