企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# C++ 程序:检查字符是元音还是辅音 > 原文: [https://www.programiz.com/cpp-programming/examples/vowel-consonant](https://www.programiz.com/cpp-programming/examples/vowel-consonant) #### 在此示例中,`if...else`语句用于检查用户输入的字母是元音还是辅音。 要理解此示例,您应该了解以下 [C++ 编程](/cpp-programming "C++ tutorial")主题: * [C++ `if`,`if...else`和嵌套`if...else`](/cpp-programming/if-else) * * * 五个字母 a,e,i,o 和 u 被称为元音。 除了这 5 个字母外,其他所有字母都是辅音。 该程序假定用户将始终输入字母。 * * * ## 示例:手动检查元音或辅音 ```cpp #include <iostream> using namespace std; int main() { char c; int isLowercaseVowel, isUppercaseVowel; cout << "Enter an alphabet: "; cin >> c; // evaluates to 1 (true) if c is a lowercase vowel isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); // evaluates to 1 (true) if c is an uppercase vowel isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); // evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true if (isLowercaseVowel || isUppercaseVowel) cout << c << " is a vowel."; else cout << c << " is a consonant."; return 0; } ``` **输出** ```cpp Enter an alphabet: u u is a vowel. ``` 用户输入的字符存储在变量`c`中。 如果`c`是小写元音,则`isLowerCaseVowel`的计算结果为`true`,而对于其他任何字符,则为`false`。 同样,如果`c`是大写元音,则`isUpperCaseVowel`的计算结果为`true`,而对于其他任何字符,则为`false`。 如果`LowerowerVowel`和`isUppercaseVowel`均为`true`,则输入的字符是元音,如果不是,则该字符是辅音。