暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

switch学习

原创 大道成佛 2021-06-08
1478

switch(表达式)//符合表达式中的条件,执行以下很多个相同变量但是值不一样,导致不同的结果的语句,break可以跳出

{

case 常量表达式1:语句1;

case 常量表达式2:语句2;

case 常量表达式3:语句3;

……

case 常量表达式n:语句n;

default:语句n+1;

}

判断闰年练习:

using System;

namespace switch_case练习
{
class Program
{
static void Main(string[] args)
{
//判断闰年练习:先输入年份;再输入月份;再输出这个月的天数;
Console.WriteLine(“请输入年份”);
try
{
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“请输入月份”);
try
{
int month = Convert.ToInt32(Console.ReadLine());
if (month >= 1 && month <= 12)
{
int day = 0;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
day = 28;
}
else
{
day = 29;
}
break;
defult: day = 30;
break;
}
Console.WriteLine("{0}年{1}月的天数是{2}", year, month, day);
}//if判断的括号
else
{
Console.WriteLine(“你输入的月份有误”);
}
}//try月份的括号
catch
{
Console.WriteLine(“你输入的月份有误,程序退出”);
}
}//try年份的括号
catch
{
Console.WriteLine(“你输入的年份有误,程序退出”);
}
Console.ReadKey ();
}
}
}

using System;

namespace switch_case语句练习
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“请输入你的姓名:”);
string name = Console.ReadLine();
switch (name)
{
case “老顾”: Console.WriteLine(“警察”);

                break;
            case "老张":Console.WriteLine("奥里给");
                
                break;
            default:
                Console.WriteLine("不认识,应该是一坨翔");
                break;
        }
        Console.ReadKey();
    }
}

}

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论