📌  相关文章
📜  Java程序检查字符是元音还是辅音

📅  最后修改于: 2021-09-04 07:53:00             🧑  作者: Mango

对于任何给定的字符,我们需要检查它是元音还是辅音。正如我们所知,元音是 ‘a’、’e’、’i’、’o’、’u’ 和所有其他字符(即 ‘b’、’c’、’d’、’f’ ….. ) 是辅音。

灯箱

例子:

Input : char = 'r'
Output : Consonant

Input : char = 'e'
Output : Vowel

在这里,在下面的实现中,我们将检查所述字符对应于五个元音中的任何一个。如果匹配,则打印“元音”,否则打印“辅音”。

示例 1:

Java
// java program to check whether input
// character is a vowel or consonant
  
import java.io.*;
  
public class geek {
  
    // Function to find whether an input
    // character is vowel or not
    static void Vowel_Or_Consonant(char y)
    {
        if (y == 'a' || y == 'e' || y == 'i' || y == 'o'
            || y == 'u')
            System.out.println("It is a Vowel.");
        else
            System.out.println("It is a Consonant.");
    }
  
    // The Driver code
    static public void main(String[] args)
    {
        Vowel_Or_Consonant('b');
        Vowel_Or_Consonant('u');
    }
}


Java
// java program to check whether input
// character is a vowel or consonant
  
import java.io.*;
  
public class geek {
  
    // Function to find whether an input
    // character is vowel or not
    static void Vowel_Or_Consonant(char y)
    {
        if (y == 'a' || y == 'e' || y == 'i' || y == 'o'
            || y == 'u' || y == 'A' || y == 'E' || y == 'I'
            || y == 'O' || y == 'U')
            System.out.println("It is a Vowel.");
        else
            System.out.println("It is a Consonant.");
    }
  
    // The Driver code
    static public void main(String[] args)
    {
        Vowel_Or_Consonant('W');
        Vowel_Or_Consonant('I');
    }
}


Java
// java program to check whether input
// character is a vowel or consonant
  
import java.io.*;
  
class GFG {
    // Function to find whether an input
    // character is vowel or not
    static String isVowel(char ch)
    {
        // Make the list of vowels
        String str = "aeiouAEIOU";
        return (str.indexOf(ch) != -1) ? "Vowel"
                                       : "Consonant";
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println("It is a " + isVowel('a'));
        System.out.println("It is a " + isVowel('x'));
    }
}


输出
It is a Consonant.
It is a Vowel.

示例 2:

  • 大写字母的更改。

Java

// java program to check whether input
// character is a vowel or consonant
  
import java.io.*;
  
public class geek {
  
    // Function to find whether an input
    // character is vowel or not
    static void Vowel_Or_Consonant(char y)
    {
        if (y == 'a' || y == 'e' || y == 'i' || y == 'o'
            || y == 'u' || y == 'A' || y == 'E' || y == 'I'
            || y == 'O' || y == 'U')
            System.out.println("It is a Vowel.");
        else
            System.out.println("It is a Consonant.");
    }
  
    // The Driver code
    static public void main(String[] args)
    {
        Vowel_Or_Consonant('W');
        Vowel_Or_Consonant('I');
    }
}
输出
It is a Consonant.
It is a Vowel.

示例 3:

  • indexOf() 方法

Java

// java program to check whether input
// character is a vowel or consonant
  
import java.io.*;
  
class GFG {
    // Function to find whether an input
    // character is vowel or not
    static String isVowel(char ch)
    {
        // Make the list of vowels
        String str = "aeiouAEIOU";
        return (str.indexOf(ch) != -1) ? "Vowel"
                                       : "Consonant";
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println("It is a " + isVowel('a'));
        System.out.println("It is a " + isVowel('x'));
    }
}
输出
It is a Vowel
It is a Consonant

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live