LogoLogo
  • V by Example
  • en
    • examples
      • section_3
        • Functions
        • Methods
        • Arrays
        • Struct
      • section_1
        • Primitives
        • Keywords
        • Variables
        • Formatted Print
        • Strings
        • Comments
      • section_4
        • Files
        • JSON
        • Testing
        • Array Functions
      • section_2
        • Operators
        • Looping Constructs
        • If statement
        • Match
  • V por exemplos
    • Contribuindo
    • examples
      • section_1
        • Comentários
        • Primitivos
        • Palavras-chave
      • section_2
        • Operadores
  • V mit einem Beispiel
    • examples
      • section_3
        • Methods
        • Struct
        • Functions
        • Arrays
      • section_4
        • Testing
        • Array Functions
        • JSON
        • Files
      • section_2
        • Looping Constructs
        • If statement
        • Match
        • Operators
      • section_1
        • Primitive Datentypen
        • Schlüsselwörter
        • Formatted Print
        • Strings
        • Kommentare
        • Variables
    • CONTRIBUTING_de
  • V dengan Contoh
    • examples
      • section1
        • Variabel
  • 通过例子学V语言
    • 例子
      • section_3
        • 函数
        • 方法
        • 数组
        • 结构体
      • section_1
        • 基本类型
        • 关键字
        • 变量
        • print系列函数
        • 字符
        • 注释
      • section_4
        • 文件
        • JSON操作
        • 调试
        • 数组函数
      • section_2
        • 运算符
        • 循环结构
        • If表达式
        • 匹配
  • コード例で学ぶV言語
    • examples
      • section_3
        • 関数
        • メソッド
        • 配列
        • 構造体
      • section_1
        • プリミティブ型
        • キーワード
        • 変数
        • 書式付き出力
        • 文字列
        • コメント
      • section_4
        • ファイル操作
        • JSON操作
        • テスト
        • 配列の関数
      • section_2
        • 演算子
        • ループ
        • If文
        • マッチ
  • Changelog
  • Contributing
  • Documentation Style Guide
Powered by GitBook
On this page
  • match语句
  • 枚举
  • 练习

Was this helpful?

  1. 通过例子学V语言
  2. 例子
  3. section_2

匹配

match语句

match语句是if-else的简写方法。 如果输入匹配,将执行第一个匹配分支的语句块,并返回其最后一个表达式。 else分支将在没有其他匹配分支时执行。

num := 1
match num % 2 == 0  {
    true { print('The input number is even.') }
    else { print('The input number is odd.') }
}

还可以使用match初始化变量,以便根据条件具有不同的值。

num := 3
s := match num {
    1 { 'one' }
    2 { 'two' }
    else {
        'many'
    }
}

例如:

fn even(num int) bool {
    match num % 2 == 0  {
        true { print('The input number is even.') }
        else { print('The input number is odd.') }
    }
}

fn num_to_str(num int) int {
    match num {
        1 { 'one' }
        2 { 'two' }
        else {
            'many'
        }
    }
}

fn main() {
    println(even(14))           // 'The input number is even.'
    println(even(3))            // 'The input number is odd.'
    println(num_to_str(1))      // 'one'
    println(num_to_str(2))      // 'two'
    println(num_to_str(352))    // 'many'
}

枚举

还可以使用variant语句将enum值(变量)作为分支进行匹配:

enum Animal {
    cat
    dog
    goldfish
    pig
}

fn makes_miau(a Animal) bool {
    return match a {
        .cat { true }
        else { false }
    }
}

fn is_land_creature(a Animal) bool {
    return match a {
        .cat { true }
        .dog { true }
        .pig { true }
        else {
            false
        }
    }
}
// OR LIKE THAT
fn is_land_creature_alt(a Animal) bool {
    return match a {
        .goldfish { false }
        else {
            true
        }
    }
}

fn main() {
    my_cat := Animal.cat
    my_goldfish := Animal.goldfish

    println(makes_miau(my_cat))             // true
    println(is_land_creature(my_cat))       // true
    println(is_land_creature(my_goldfish))  // false
}

练习

1.编写一个V程序,创建一个从1到50的所有偶数数组。 2.编写一个V程序,给定一个数字数组,返回最大值。 3.编写一个V程序来确定颜色(枚举)是红色还是蓝色

PreviousIf表达式Nextコード例で学ぶV言語

Last updated 5 years ago

Was this helpful?