文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > 计算机软件及应用 > C#switch-case语句示例
侵权投诉

C#switch-case语句示例

//C#switch-case语句示例 2009-10-01 23:01:01| 分类: 软件专业技术 | 标签: |字号大中小 订阅 .

using System;
using System.Collections.Generic;
using System.Text;

namespace timegood
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("************Time**********");
Console.WriteLine("\t1、morning");
Console.WriteLine("\t2、afternoon");
Console.WriteLine("\t3、night");
Console.Write ("请选择时间:");
string time = Console.ReadLine();
switch(time)
{
case "1":
Console.WriteLine("Good morning!");
break;
case "2":
Console.WriteLine("Good afternoon!");
break;
case "3":
Console.WriteLine("Good night!");
break;
default:
Console.WriteLine("Selection error!");
break;
}
}
}
}

----------------------------------------------
C# switch case的学习 doc.guandang.net

1 switch case 只能对离散的值进行判断。在case处只能写常量。

2 与C,C++ 不同,C#中的switch不允许遍历,必须在每个case后面加break或者goto的语句,否则编译不会通过。

3 值得注意的一点:C#是可以把字符串当常量处理的,这个和C和C++是有区别的。

所以switch()括号中的控制可以是string类型的。

4 在极特殊的情况下,当好几个case的处理方式是一样的时候,可以采取下面的形式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 转义字符
{
class Program
{
static void Main(string[] args)
{
int i = Int32.Parse(Console.ReadLine());

switch(i)
{
case 1:
Console.WriteLine("haha");
break;
case 2:
case 3:
case 4:
case 5:
Console.WriteLine("heihei");
break;
default:
Console.WriteLine("NO");
break;
}
}
}
}
这里的case等于2,3,4,5的处理方式是一样的。

5 default:后也需要break;

6 switch语句一般都可以用if重写,但是if语句不一定能用switch重写。

----------------------------------------------------------------------
一下这段用VB的代码,如果用C#写应该如何些呢?doc.guandang.net

Function getDaysInMonth(strMonth,strYear)
Dim strDays
Select Case cint(strMonth)
Case 1,3,5,7,8,10,12:
strDays = 31
Case 4,6,

第1页

你可能喜欢

  • C#入门经典
  • 单片机原理及应用
  • C#基本语法
  • 经典教程
  • 毛概总结
  • while语句
  • for循环语句
  • while循环

热门文档

相关文档

  • 例题3 4和switch--case语句

    例题3 4和switch--case语句_IT/计算机_专业资料。例题3 4和switch--case... C#switch-case语句示例 3页 免费 C语言:switch case语句... 1页 1下载券 ...

  • switch-case语句用法

    ("70-84\n"); break; case 'C': printf("60-69\n"); break; case ...此时,switch语句相当于一个空语句.例如,将上面例子中switch语句中的default部分...

  • MATLAB+switch+case+语句使用实例+源程序

    MATLAB+switch+case+语句使用实例+源程序_计算机软件及应用_IT/计算机_专业资料... case {3,4,5} season='spring' case {6,7,8} season='summer' c...

  • 二级C语言考试必备(真题)考点18 switch-case 语句

    二级C语言考试必备(真题)考点18 switch-case 语句_IT认证_资格考试/认证_教育... C语言:switch case语句... 1页 1下载券 C#switch-case语句示例 3页 免费 ...

  • Java_switch-case语句用法总结

    原理归原理,下面是几个容易混淆的例子. 1.标准型(case 后面都有 break 语句) int i=3; switch(i) { case 1: System.out.println(1); break; case 2:...

  • C++ switch-case语句应用经验讲解

    C++ switch-case语句应用经验讲解_计算机软件及应用_IT/计算机_专业资料。C++ switch-case 语句应用经验讲解 C++ switch-case 语句是一个循环判断语句,在实际编程...

  • 计算机二级C语言 考点18 switch-case 语句

    计算机二级C语言 考点18 switch-case 语句_IT认证_资格考试/认证_教育专区。计算机二级C语言 考点18 switch-case 语句1、若有定义: float w; int a, b; 则...

  • C#学习之分支语句

    C#学习之分支语句_计算机软件及应用_IT/计算机_专业资料。《C#学习之分支语句》一文中主要介绍了if-else语句和switch-case语句。今日推荐 ...

  • C#流程控制语句_条件结构详解

    多分支选择语句 if…else if… 多分支选择语句示例: double examMark=4; //...switch-case 语句的语法为: 19 4.多分支选择语句 switch() switch (表达式) ...

  • 计算机二级C语言__考点18_switch-case_语句

    计算机二级C语言__考点18_switch-case_语句_IT认证_资格考试/认证_教育专区。...int n=’c’; switch(n++) { default: printf(“error”);break; case ...

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email