VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之SWITCH练习(一年第几天的判断)

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

复制代码
using System;
namespace program
{
    class program1
    {
        static void Main(string[] args)
        {
            program1 fenshu = new program1();
            fenshu.Panduan();
            program1 tian = new program1();
            tian.Tianshu();
        }
        /// <summary>
        /// 计算一年中的第几天
        /// </summary>
        void Tianshu()
        {
            Console.Write("请输入年份=");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入月份=");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入日=");
            int c = Convert.ToInt32(Console.ReadLine());
            int total = 365;
            if ((total % 4 == 0 && total % 100 != 0) || (total % 400 == 0))//是否闰年的判断
            {
                total = 366;
            }
            switch (b)
            {
                case 1: total -= 31; goto case 2;
                case 2:
                    if ((total % 4 == 0 && total % 100 != 0) || (total % 400 == 0))//闰年二月份有29天                    
                        total -= 29;    //一层可以不使用块                
                    else                    
                        total -= 28;                    
                    goto case 3;
                case 3: total -= 31; goto case 4;
                case 4: total -= 30; goto case 5;
                case 5: total -= 31; goto case 6;
                case 6: total -= 30; goto case 7;
                case 7: total -= 31; goto case 8;
                case 8: total -= 31; goto case 9;
                case 9: total -= 30; goto case 10;
                case 10: total -= 31; goto case 11;
                case 11: total -= 30; goto case 12;
                case 12: total -= 31; goto default;
                default: total += c; break;
            }
            Console.WriteLine("{0}年{1}月{2}日是一年的第{3}天", a, b, c, total);
        }
        /// <summary>
        /// 练习switch
        /// </summary>
        void Panduan()
        {
            Console.Write("请输入学生等级的值=");
            string a = Console.ReadLine();
            switch (a)
            {
                case "A": Console.WriteLine("85-100"); break;
                case "B": Console.WriteLine("70-84  "); break;
                case "C": Console.WriteLine("60-69"); break;
                case "D": Console.WriteLine("不及格"); break;
                default: Console.WriteLine("输入有误"); break;
            }
        }
    }
}
复制代码
相关教程