C# 实验七

第一题:编写一个控制台程序,输入一个日期,输出这一天是星期几。

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入日期(格式xxxx-xx-xx):");
            string date = Console.ReadLine();
            DateTime dt = Convert.ToDateTime(date);
            string week = dt.DayOfWeek.ToString();
            Console.WriteLine("Today is "+week);
        }
}

运行截图

C# 实验七

第二题:输入你的生日

private void button1_Click(object sender, EventArgs e)
        {
            // string date = textBox1.Text + "-" + textBox2.Text + "-" + textBox3.Text;
            string input_date = dateTimePicker1.Text;
            // Console.WriteLine(input_date);
            DateTime dt = Convert.ToDateTime(input_date);
            int y = dt.Year;
            int m = dt.Month;   // 获取月
            int d = dt.Day;     // 获取日
            string date = m.ToString() + "-" + d.ToString();
            DateTime birth = Convert.ToDateTime(date);
            DateTime today = DateTime.Today.Date;
            string week = dt.DayOfWeek.ToString();
            if (DateTime.Compare(birth, today) > 0)
            {
                label5.Text = "您的"+(today.Year-y)+"岁生日还有" + (birth - today).Days + "天,那天是" + week;
            }
            if (DateTime.Compare(birth, today) == 0)
            {
                label5.Text = "祝你"+(today.Year-y)+"生日快乐!";
            }
            if (DateTime.Compare(birth, today) < 0)
            {
                label5.Text = "您的" + (today.Year - y) + "生日已过" + (today - birth).Days + "天,那天是" + week;
            }
            
        }

运行截图

C# 实验七
C# 实验七
C# 实验七
C# 实验七

第三题:车牌摇号

// 生成随机车牌
        public string startNumer(Random r)
        {
            char E1 = (char)r.Next(65, 90);
            char E2 = (char)r.Next(65, 90);
            int N1 = r.Next(10, 99);
            string n1 = N1.ToString();
            int N2 = r.Next(9);
            string n2 = N2.ToString();
            string number = "" + E1 + n1 + E2 + n2;
            return number;
        }

        // 为每个单选框的text赋值显示车牌信息
        public void showNumber()
        {
            RadioButton[] radioButtons = new RadioButton[10];
            radioButtons[0] = radioButton1;
            radioButtons[1] = radioButton2;
            radioButtons[2] = radioButton3;
            radioButtons[3] = radioButton4;
            radioButtons[4] = radioButton5;
            radioButtons[5] = radioButton6;
            radioButtons[6] = radioButton7;
            radioButtons[7] = radioButton8;
            radioButtons[8] = radioButton9;
            radioButtons[9] = radioButton10;
            Random r = new Random();    // 随机数种子不能写在循环中,否者会出现高并发的情况
            foreach (RadioButton rb in radioButtons)
            {
                rb.Text = startNumer(r);
                rb.Visible = true;
            }
        }



        // 开始摇号
        int times = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            if (times < 5)
            {
                showNumber();
                times++;
            }
            else
            {
                MessageBox.Show("您已经用完摇号次数!");
            }
        }

        // 判断选中
        string hNumer;
        private void Radio_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            hNumer = rb.Text;
            /*if (rb.Checked)
            {
                MessageBox.Show("您选择的号码为 " + rb.Text);
            }*/
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("您已选择号码为 " + hNumer);
            Application.Exit();         // 关闭应用
        }

运行截图

  • 初始界面
    C# 实验七
  • 开始摇号
    C# 实验七
    选择号码并确定
    C# 实验七

第四题:扑克牌游戏

class Program
    {
        static int[] cards = new int[52];  // 存牌数组
        static int times;
        // 生成扑克牌
        public static void initCard()
        {
            
            
            int k = 0;
            // 生成扑克牌并存到牌盒中
            for (int i = 1; i <= 4;i++)
            {
                for (int j = 1;j <= 13;j++)
                {
                    int temp = 100;
                    temp *= i;
                    temp += j;
                    cards[k] = temp;
                    k++;
                }
                // 测试代码
                // Console.WriteLine();
            }

            // 测试代码
            //foreach(int i in cards)
            //{
            //    Console.WriteLine(i+" ");
            //}
            // 测试结果:生成模块通过
        }

        // 洗牌
        public static TimeSpan washCard()
        {
            // 控制数字输入   
            Boolean input = true;
            while (input)
            {
                Console.Write("How many times for card:");
                try
                {
                    times = int.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("请输入正整数!");
                    Console.ReadKey();
                    Console.Clear();
                    washCard();
                }
                if (times <= 0)
                {
                    Console.WriteLine("请输入正整数!");
                }
                else
                {
                    input = false;
                }  
            }

            /*  洗牌算法
             *  目的:将数组中的下标随机打乱
             *  步骤:
             *      1.生成随机数下标r1;
             *      2.生成随机数下标r2;
             *      3.用temp接收r1的值;
             *      4.r1接收r2的值;
             *      5.r2接收temp的值;
             *      完成换值操作
             *  注意事项:
             *      1.两个随机数下标不能相同
             *      2.洗牌一次的意思是更换20张牌的值(初步决定,后期视情况更改)
             */

            // 
            Random rd = new Random();
            int time_temp = times;
            TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
            while (time_temp > 0)
            {
                //洗一次牌
                for (int i = 0; i < 20; i++)
                {
                    int r1 = rd.Next(52);
                    int r2 = rd.Next(52);
                    while (r1 == r2)
                    {
                        r2 = rd.Next(52);
                    }
                    int temp = cards[r1];
                    cards[r1] = cards[r2];
                    cards[r2] = temp;
                }
                time_temp--;
            } 
            TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan ts = ts2.Subtract(ts1).Duration();         // 时间差
            return ts;
        }

        // 发牌
        public static void emitCard()
        {
            //Console.WriteLine("进入发牌");
            int[] player1 = new int[13];
            int[] player2 = new int[13];
            int[] player3 = new int[13];
            int[] player4 = new int[13];
            // 开始发牌
            int count = 0;
            for (int j = 0;j < 13;j++)
            {
                //Console.WriteLine(i+" ");
                player1[j] = cards[count++];
                player2[j] = cards[count++];
                player3[j] = cards[count++];
                player4[j] = cards[count++];
            }

            //Console.WriteLine("发牌完成");
            // 明牌
            Console.WriteLine("玩家1的牌:");
            foreach (int i in player1)
            {
                string stemp = fiter(i);
                Console.Write(stemp + " " );
            }
            Console.WriteLine();

            Console.WriteLine("玩家2的牌:");
            foreach (int i in player2)
            {
                string stemp = fiter(i);
                Console.Write(stemp + " ");
            }
            Console.WriteLine();

            Console.WriteLine("玩家3的牌:");
            foreach (int i in player3)
            {
                string stemp = fiter(i);
                Console.Write(stemp + " ");
            }
            Console.WriteLine();

            Console.WriteLine("玩家4的牌:");
            foreach (int i in player4)
            {
                string stemp = fiter(i);
                Console.Write(stemp + " ");
            }
            Console.WriteLine();
        }

        // 过滤器
        public static string fiter(int num)
        {
            /* 规则:
             * 判断花色:
             * 大于100小于200为红桃,ASCII码:003
             * 大于200小于300为方块,ASCII码:004
             * 大于300小于400为梅花,ASCII码:005
             * 大于400为黑桃,ASCII码:006
             * 
             * 判断值
             * 1为A
             * 11为J
             * 12为Q
             * 13为K
             *
             * 注意事项:
             *  先判断花色再判断值
             *  用字符串输出
             */

            string cardType = "";
            if (100 < num && num < 200)
            {  
                cardType += "♥";
                num -= 100;
            }else if (200 < num && num < 300)
            {
                cardType += "♦ ";
                num -= 200;
            }
            else if (300 < num && num < 400)
            {
                cardType += "♣";
                num -= 300;
            }
            else if (400 < num)
            {
                cardType += "♠";
                num -= 400;
            }
            // 判断牌值
            if (num == 1)
            {
                cardType += "A";
            }
            else if (num == 11)
            {
                cardType += "J";
            }
            else if (num == 12)
            {
                cardType += "Q";
            }
            else if (num == 13)
            {
                cardType += "K";
            }else
            {
                cardType += num;               
            }
            // 排版
            cardType += " ";
            if(num != 10)
            {
                cardType += " ";
            }
            return cardType;
        }

        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8; // 能够输出花色的关键
            //Console.WriteLine("开始");
            initCard();
            //Console.WriteLine("初始化牌成功,正洗牌");
            TimeSpan ts = washCard();
            Console.WriteLine("洗牌完成\n");
            emitCard();
            Console.WriteLine("\n洗牌"+times+"次用时:"+ts.Milliseconds.ToString()+" 毫秒\n");        
        }
    }

运行截图

C# 实验七

上一篇:C#与Java的长度计算API的区别


下一篇:C#实现简单学生信息管理(极简)