VB.Net - Select Case语句

2018-12-16 13:45 更新

Select Case语句允许根据值列表测试变量的相等性。 每个值称为大小写,并且为每个选择大小检查打开的变量。


语法:

VB.Net中Select Case语句的语法如下:
Select [ Case ] expression
    [ Case expressionlist
        [ statements ] ]
    [ Case Else
        [ elsestatements ] ]
End Select

  • expression: is an expression that must evaluate to any of the elementary data type in VB.Net, i.e., Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, SByte, Short, Single, String, UInteger, ULong, and UShort.是必须求值到VB.Net中的任何基本数据类型的表达式,即布尔值,字节,字符,日期,双精度,小数,整数,长整型,对象,SByte,短,单,字符串,UInteger, ULong和UShort。

  • expressionlist: List of expression clauses representing match values forexpression. Multiple expression clauses are separated by commas.表达式子句表示表达式匹配列表逗号分隔多个表达式子句

  • statements: statements following Case that run if the select expression matches any clause in expressionlist.语句如果select表达式匹配expressionlist中的任何子句,则运行的案例。

  • elsestatements: statements following Case Else that run if the select expression does not match any clause in the expressionlist of any of the Case statements.如果select表达式与任何Case语句的expression列表中的任何子句不匹配,则运行Case Else后的语句。


流程图:


流程图

示例:

Module decisions
   Sub Main()
      'local variable definition
      Dim grade As Char
      grade = "B"
      Select grade
          Case "A"
              Console.WriteLine("Excellent!")
          Case "B", "C"
              Console.WriteLine("Well done")
          Case "D"
              Console.WriteLine("You passed")
          Case "F"
              Console.WriteLine("Better try again")
          Case Else
              Console.WriteLine("Invalid grade")
      End Select
      Console.WriteLine("Your grade is  {0}", grade)
      Console.ReadLine()
   End Sub
End Module

当上述代码被编译和执行时,它产生以下结果:
Well done
Your grade is B

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部