Java 使用 switch 语句确定用户输入是元音还是不是元音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21689741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:04:42  来源:igfitidea点击:

Determine the user input is either a vowel or not using switch statement

javaswitch-statement

提问by user3294763

public class vowel { 
     private static int ch;    
     public static void main(String[]args){     
        char vowel;
        Scanner sc = new Scanner(System.in);     
        System.out.println("Enter alphabet:" );     
        vowel=sc.next().charAt(0);

    switch (ch){    
        case 'a':     
        case 'A':     
        case 'e':     
        case 'E':     
        case 'i':     
        case 'I':     
        case 'o':     
        case 'O':     
        case 'u':     
        case 'U':    
           System.out.println("This is a Vowel:"+ vowel);
           break;
        default:
           System.out.println("This is not a Vowel:"+ vowel);
           break;

        }
    } 
}

the problem is that no matter what letter i enter, it will always have an outcome of saying 'This is not a Vowel' although it is.

问题是,无论我输入什么字母,它总是会说“这不是元音”,尽管它是。

采纳答案by takendarkk

Since vowelis the letter you are looking at, you need to add that to your switch statement. The first code you provided didn't know what variable you were using for comparison.

由于vowel是您正在查看的字母,因此您需要将其添加到 switch 语句中。您提供的第一个代码不知道您使用什么变量进行比较。

switch(vowel){ //You need something here.
    case 'a':     
    case 'A':
    // continue with other vowels 
        System.out.println("This is a Vowel:"+ vowel);
        break;
    default:
        System.out.println("This is not a Vowel:"+ vowel);
        break;
}

Don't switch on ch, you aren't even using that in the code you provided. Unless you are using that somewhere else in your code, you can remove it completely.

不要打开ch,你甚至没有在你提供的代码中使用它。除非您在代码中的其他地方使用它,否则您可以完全删除它。



EDIT编辑

If you want to look at a whole string and check each char for whether or not it is a vowel try something like this

如果您想查看整个字符串并检查每个字符是否是元音,请尝试这样的操作

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);     
    System.out.print("Enter a word: " ); //Better prompt IMO
    String str = sc.next(); //Get the whole string
    char[] myChar = str.toCharArray(); //Turn the string into an array of char

    for (char c : myChar) { //For every char in the array
        switch (c) { //Check if it is a vowel or not
            case 'a':     
            case 'A':     
            case 'e':     
            case 'E':     
            case 'i':     
            case 'I':     
            case 'o':     
            case 'O':     
            case 'u':     
            case 'U':    
                System.out.println(c + " - Vowel"); //Easier for me to read
                break;
            default:
                System.out.println(c);
                break;
        }
    }
}