当我键入辅音时,它没有意识到有一个if语句。
而且当我键入e时,它意识到它是辅音。

另外,当我输入“ a”时,它会为字符串元音和else语句生成if语句。
与大写字母“ A”相同,但这次它两次产生else。

import javax.swing.JOptionPane;

public class R
{
    public static void main(String args[])
    {
        String[] vowels = {"a","e","i","o","u"};
        String[] vowel = {"A","E","I","O","U"};
        String InputVowel = JOptionPane.showInputDialog(null,"Enter a Character: ");
        for (int x=0;x<vowels.length;x++)
        {
            if(InputVowel.equals (vowels[x]))
                JOptionPane.showMessageDialog(null,InputVowel+" is a lowercase");
            if(InputVowel.equals(vowel[x]))
                JOptionPane.showMessageDialog(null,InputVowel+" is an uppercase");
            else
                x = 5;
                JOptionPane.showMessageDialog(null,InputVowel+" is a consaunant");
        }
    }
}

最佳答案

在检查所有候选(所有元音)后,您只能假定它不是元音

    String[] vowels = {"a","e","i","o","u"};
    String[] vowel = {"A","E","I","O","U"};
    String InputVowel = JOptionPane.showInputDialog(null,"Enter a Character: ");
    boolean isVowel = false;

    for (int x=0;x<vowels.length;x++)
    {
        if(InputVowel.equals (vowels[x])) {
            JOptionPane.showMessageDialog(null,InputVowel+" is a lowercase");
            isVowel = true;
            break;
        }
        if(InputVowel.equals(vowel[x])) {
            JOptionPane.showMessageDialog(null,InputVowel+" is an uppercase");
            isVowel = true;
            break;
        }
    }

    if (!isVowel)
        JOptionPane.showMessageDialog(null,InputVowel+" is an consaunant");

09-30 15:07
查看更多