• switch结构2016/03/08


    Switch  03/08

    一、结构

    switch(){  case *: ;break;……default: ;brek;}

    练习:输入一个日期,判断这一年第几天?

    Console.Write("请输入月份");
    int m = int.Parse(Console.ReadLine());
    Console.Write("请输入几号");
    int d = int.Parse(Console.ReadLine());
    int m1 = 31, m2 = 28, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30;
    switch (m)
    {
    case 1:
    Console.WriteLine("第" + (d) + "天");
    break;
    case 2:
    Console.WriteLine("第" + (m1 + d) + "天");
    break;
    case 3:
    Console.WriteLine("第" + (m1 + m2 + d) + "天");
    break;
    case 4:
    Console.WriteLine("第" + (m1 + m2 + m3 + d) + "天");
    break;
    case 5:
    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + d) + "天");
    break;
    case 6:
    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + d) + "天");
    break;
    case 7:
    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + d) + "天");
    break;
    case 8:
    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + d) + "天");
    break;
    case 9:
    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + d) + "天");
    break;
    case 10:
    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + d) + "天");
    break;
    case 11:
    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + d) + "天");
    break;
    case 12:
    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + d) + "天");
    break;
    default:
    Console.WriteLine("你输入的有误!");
    break;
    }

    二、判断闰年,普通年份,是4的倍数但不是100的倍数;世纪念是400的倍数(year%4==0&&year%100!=0)||year%100==0
    Console.Write("请输入年份:");
    int year = int.Parse(Console.ReadLine());
    if (year >= 0 && year <= 9999)
    {
    if ((year % 4 == 0 && year % 100 != 0) || year % 100 == 0)
    {
    Console.WriteLine("你输入的是闰年。");
    }
    else
    {
    Console.WriteLine("你输入的是平年。");
    }
    }
    else
    {
    Console.WriteLine("你输入的年份有误!");
    }

    三、任意输入一个0——9999之间的年份,并正确输入正确的日期,最后在正确输出这个日期,否则显示输入的格式错误。

    Console.Write("请输入年份:");
    int y = int.Parse(Console.ReadLine());
    Console.Write("请输入月份:");
    int m = int.Parse(Console.ReadLine());
    Console.Write("请输入几号:");
    int d = int.Parse(Console.ReadLine());
    if (y >= 0 && y <= 9999)
    {

    if (m >= 1 && m <= 12)
    {
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
    {

    if (d >= 1 && d <= 31)
    {
    Console.Write("你输入的是" + y + "年" + m + "月" + d + "号");
    }
    else
    {
    Console.WriteLine("你输入的几号格式错误。");
    }
    }
    else if (m == 4 || m == 6 || m == 9 || m == 11)
    {

    if (d >= 1 && d <= 30)
    {
    Console.Write("你输入的是" + y + "年" + m + "月" + d + "号");
    }
    else
    {
    Console.WriteLine("你输入的几号格式错误。");
    }
    }
    else//m==2
    {
    if ((y % 4 == 0 && y % 100 != 0) || y % 100 == 0)
    {

    if (d >= 1 && d <= 29)
    {
    Console.Write("你输入的是" + y + "年" + m + "月" + d + "号");
    }
    else
    {
    Console.WriteLine("你输入的几号格式错误。");
    }
    }
    else
    {

    if (d >= 1 && d <= 28)
    {
    Console.Write("你输入的是" + y + "年" + m + "月" + d + "号");
    }
    else
    {
    Console.WriteLine("你输入的几号格式错误。");
    }
    }
    }
    }
    else
    {
    Console.WriteLine("你输入的月份格式错误。");
    }
    }
    else
    {
    Console.WriteLine("你输入的年份格式错误。");
    }

  • 相关阅读:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getDataSource' defined in class path resource nested exception is org.springframework.beans.factory.BeanCreat异常
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating Caused by: tk.mybatis.mapper.MapperException: 无法获取 cn.itcast.mapper.UserMapper.existsWithPrimaryKey 方法的泛型信息!
    fibnacci数列递归实现
    XOR加密
    Pep8课下作业
    求最大公约数伪代码
    《信息安全专业导论》第五周学习总结
    20191306《信息安全专业导论》第四周学习总结
    用或非门实现其他逻辑门
    寻找黑客偶像
  • 原文地址:https://www.cnblogs.com/zst062102/p/5256033.html
Copyright © 2020-2023  润新知