Go語言程式檢查字母是母音還是子音


在本教程中,我們將編寫一段 Go 語言程式碼來檢查字母是母音還是子音。

母音和子音的區別

如果字元在以下字元列表中 {a, e, I, o, u},則稱為母音。

所有其他字元稱為子音。在英語中,有 5 個母音和 21 個子音。

這裡我們將使用以下 2 種方法 -

方法 1:使用 if/else 語句

在這種方法中,我們將使用條件語句來檢查字元是母音還是子音。

方法 2:使用 switch case

在這種方法中,我們將使用 switch 來檢查字母是母音還是子音。

示例 1:Go 語言程式檢查給定字元是母音還是子音

語法

if condition {
   //code
} else {
   //code
}

如果條件用於檢查特定條件,並且僅在條件滿足時才執行程式碼行。

我們可以使用 if-else 條件語句檢查多個條件,如果第一個條件滿足則執行一組程式碼,如果第二個條件滿足則執行第二組程式碼。

演算法

  • 步驟 1 - 匯入包 fmt

  • 步驟 2 - 初始化並定義 isVovel() 函式。

  • 步驟 3 - 啟動 main() 函式。

  • 步驟 4 - 初始化字串資料型別變數,並在其中儲存一個字元。

  • 步驟 5 - 呼叫 isVovel() 函式。

  • 步驟 6 - 將其結果儲存在變數中。

  • 步驟 7 - 在螢幕上列印結果。

示例

//GOLANG PROGRAM TO CHECK IF AN ALPHABET IS A VOWEL OR CONSONANT package main // fmt package provides the function to print anything import "fmt" // initialize and define a isVowel() function func isVowel(character rune) { // conditional statements to check if a character is vowel if character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' { // printing the vowels fmt.Printf("Input Character = %c \n It is a vowel\n", character) } else { // print a consonent fmt.Printf("Input character = %c \n It is a consonent \n", character) } } // starting the main function func main() { // calling the isVowel() function and passing character to it as arguments. isVowel('e') // vowel isVowel('b') // consonant }

輸出

Input Character = a 
It is a vowel
Input character = b 
It is a consonent 

程式碼描述

1. 首先,我們必須匯入 fmt 包,該包允許我們在螢幕上列印任何內容。

2. 然後我們定義了 isVowel() 函式,其中將包含我們檢查字元是母音還是子音的邏輯。

3. 此函式包含 if-else 條件語句來比較字元,然後在螢幕上列印相應的結果。

4. 啟動 main() 函式。

5. 透過將特定字元作為引數傳遞給它來呼叫 isVowel() 函式。

6. 對字元進行母音檢查,如果提供的字元是母音,則在螢幕上列印給定字元是母音,否則列印給定字元是子音。

示例 2:使用 Switch Case 檢查字母是母音還是子音的 Go 語言程式

語法

switch optstatement; optexpression{ 
   case expression1: Statement. 
   case expression2: Statement. 
   ... 
   default: Statement. 
}

演算法

  • 步驟 1 - 匯入包 fmt

  • 步驟 2 - 初始化並定義 isVovel() 函式。

  • 步驟 3 - 使用 switch case 列印母音和子音。

  • 步驟 4 - 啟動 main() 函式。

  • 步驟 5 - 呼叫 isVovel() 函式。

  • 步驟 6 - 使用 fmt.Printf() 函式在螢幕上列印結果。

示例

// GOLANG PROGRAM TO CHECK THE ALPHABET IS VOWEL OR CONSONANT USING SWITCH package main // fmt package provides the function to print anything import "fmt" // defining the function with a parameter of rune func isVowel(character rune) { // Using the switch statement switch character { // defining the switch casses case 'a', 'e', 'i', 'o', 'u': // printing the output fmt.Printf("The provided character %c is a vowel\n", character) // defining the default case default: fmt.Printf("The provided character %c is a consonant\n", character) } } // main fuction func main() { //call the isVowel() function isVowel('n') isVowel('o') }

輸出

The provided character n is a consonant
The provided character o is a vowel

程式碼描述

1. 首先,我們必須匯入 fmt 包,該包允許我們在螢幕上列印任何內容。

2. 然後我們定義了 isVowel() 函式,其中將包含我們檢查字元是母音還是子音的邏輯。

3. 此函式包含 switch case 來比較字元,然後在螢幕上列印相應的結果。

4. 第一個 case 是如果將母音中的任何一個作為函式的引數傳遞,則執行一組程式碼,並在螢幕上列印相應的訊息,即提供的字元是母音。

5. 然後我們需要定義 default case。如果傳遞的字元不是母音,則執行 default case,並在螢幕上列印結果,即提供的字元是子音。

6. 啟動 main() 函式。

7. 透過將特定字元作為引數傳遞給它來呼叫 isVowel() 函式。

8. 對字元進行母音檢查,如果提供的字元是母音,則在螢幕上列印給定字元是母音,否則列印給定字元是子音。

結論

在以上兩個示例中,我們已成功編譯並執行了 Go 語言程式以檢查字母是母音還是子音。我們在此程式中使用了 2 種不同的方法。

在第一種方法中,我們使用了條件語句,並將字母與母音 {a, e, i, o, u} 進行了比較。

在第二種方法中,我們使用了 switch case 來檢查給定字元是母音還是子音。

更新於: 2023年4月4日

833 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.