Golang 检查一个字母是元音还是辅音
在本教程中,我们将编写一个Go语言代码来检查一个字母是元音还是辅音。
元音和辅音的区别
如果一个字符在以下字符列表中的任何一个中,它被称为元音字符:{a, e, I, o, u}。
其余的所有字符都被称为辅音字符。在英语中,有5个元音字符和21个辅音字符。
这里我们将使用以下两种方法:
方法1:使用if/else语句
在这种方法中,我们将使用条件语句来检查一个字符是元音还是辅音。
方法2:使用switch case
在这种方法中,我们将使用switch来检查一个字母是元音还是辅音。
示例1:Golang程序,检查给定字符是元音还是辅音
语法
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检查字母是元音还是辅音的Golang程序
语法
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. 第一个情况是当任何一个元音作为函数参数传递时,执行一组代码,并在屏幕上打印出所提供字符是元音的消息。
5. 然后我们需要定义默认情况。如果传递的字符不是元音,则执行默认情况,结果将在屏幕上打印出所提供字符是辅音的消息。
6. 开始 main() 函数。
7. 通过将特定字符作为参数传递给 isVowel() 函数来调用它。
8. 根据元音字符检查字符,如果提供的字符是元音,则打印在屏幕上给定字符是元音,否则打印给定字符是辅音。
结论
在上面的两个示例中,我们成功编译并执行了用于检查字母是元音还是辅音的Golang程序。我们在该程序中使用了2种不同的方法。
在第一种方法中,我们使用条件语句,并将字母与元音字符{a, e, i, o, u}进行比较。
在第二种方法中,我们使用switch case来检查给定的字符是元音还是辅音。