有没有更好的方法来编写与我的代码相同的代码?

时间:2019-09-15 04:04:56

标签: java if-statement

我想将此代码编写为元音查找器,以排除数字和符号或符号,除了将它们一个接一个地添加之外,还有更好的方法吗?该代码工作正常,但我认为有更好的方法可以做到这一点。我是Java的新手,我仅在2周前就学会了。

import java.util.Scanner;
public class ConsonantOrVowel {
    public static void main(String[] args) {
        char c;
        System.out.println("Please Input a Character: ");
        Scanner input = new Scanner(System.in);
        c=input.next().charAt(0);
        if(c == '1'|| c == '2'|| c == '3'|| c == '4'|| c == '5' ||c == '6' ||c == '7'||c == '8'||c == '9'|| c == '!'|| c == '@' ||c == '#' ||c == '$'||c == '%'||c == '^'|| c == '&'|| c == '*' ||c == '(' ||c == ')'||c == '_'||c == '+'|| c == '-'|| c == '/'||c == '['|| c == ']'|| c == '{' ||c == '}' ||c == ';'||c == ':'||c == '"'|| c == ','|| c == '.'|| c == '?'){
            System.out.println("Invalid Input");
        }
        else if(c == 'A'|| c == 'a'|| c == 'E' ||c == 'e' ||c == 'I'||c == 'i'||c == 'O'||c == 'o'||c == 'U'||c == 'u') {
            System.out.println("Input Character is a Vowel");
        }
        else
            System.out.println("Input Character is a Consonant");
    }
}

3 个答案:

答案 0 :(得分:3)

您可以使用String.inxedOf(char c)

public class ConsonantOrVowel {
    public static void main(String[] args) {
        char c;
        System.out.println("Please Input a Character: ");
        Scanner input = new Scanner(System.in);
        c = input.next().charAt(0);

        if ("123456789!@#$%^&*()_+-/[]{};:\",.?".indexOf(c) > -1) {
            System.out.println("Invalid Input");
        } else if ("AaEeIiOoUu".indexOf(c) > -1) {
            System.out.println("Input Character is a Vowel");
        } else
            System.out.println("Input Character is a Consonant");
    }
}

答案 1 :(得分:1)

列出旧数组

您可以先使用一些数组...

char[] exclude = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '/', '[', ']', '{', '}', ';', ':', '"', ',', '.', '?'};
char[] include = new char[]{'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u'};

System.out.println("Please Input a Character: ");
Scanner input = new Scanner(System.in);
char c = input.next().charAt(0);
boolean shouldExclude = false;
for (char value : exclude) {
    if (value == c) {
        shouldExclude = true;
        break;
    }
}
if (shouldExclude) {
    System.out.println("Invalid Input");
} else {
    boolean isIncluded = false;
    for (char value : include) {
        if (value == c) {
            shouldExclude = true;
            break;
        }
    }
    if (isIncluded) {
        System.out.println("Input Character is a Vowel");
    } else {
        System.out.println("Input Character is a Consonant");
    }
}

这有点长,但是很容易更新

java.util.List

您可以使用collections API中的List类,该类具有contains方法,该方法使检查List的匹配元素变得更加容易...

Character[] exclude = new Character[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '/', '[', ']', '{', '}', ';', ':', '"', ',', '.', '?'};
Character[] include = new Character[]{'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u'};

List<Character> excludeList = Arrays.asList(exclude);
List<Character> includeList = Arrays.asList(include);

System.out.println("Please Input a Character: ");
Scanner input = new Scanner(System.in);
char c = input.next().charAt(0);
if (excludeList.contains(c)) {
    System.out.println("Invalid Input");
} else if (includeList.contains(c)) {
    System.out.println("Input Character is a Vowel");
} else {
    System.out.println("Input Character is a Consonant");
}

使用String

好的,这类似于使用List,但是因为我们只对单个字符匹配感兴趣,所以设置起来稍微容易一些

String exclude = "123456789!@#$%^&*()_+-/[]{};:\",.?";
String include = "AaEeIiOoUu";

System.out.println("Please Input a Character: ");
Scanner input = new Scanner(System.in);
char c = input.next().charAt(0);
String value = Character.toString(c);
if (exclude.contains(value)) {
    System.out.println("Invalid Input");
} else if (include.contains(value)) {
    System.out.println("Input Character is a Vowel");
} else {
    System.out.println("Input Character is a Consonant");
}

正则表达式

这有点复杂,但是功能非常强大(正则表达式是您应该花些时间学习的东西)

String excludePattern = "[0-9\\!@#\\$\\^&\\*\\(\\)_+-/\\[\\]{};:\",\\.\\?%]";
String includePattern = "[AaEeIiOoUu]";

System.out.println("Please Input a Character: ");
Scanner input = new Scanner(System.in);
char c = input.next().charAt(0);
String value = Character.toString(c);
if (value.matches(excludePattern)) {
    System.out.println("Invalid Input");
} else if (value.matches(includePattern)) {
    System.out.println("Input Character is a Vowel");
} else {
    System.out.println("Input Character is a Consonant");
}

可能还有其他我没想过的方式...

正如我所展示的,解决/解决此问题的方法不止一种。您使用哪个(一个或多个)取决于您要实现的目标,可用的输入以及所需的输出

答案 2 :(得分:1)

IMO,您的代码的真正问题是这是不正确的。

    else
        System.out.println("Input Character is a Consonant");

这是一个根本上不正确的假设。您假设所有不在前两个集合中的字符必须是辅音。

  • SP(空格),HT(制表符)和其他各种控制字符怎么办?
  • 关于LATIN-1重音字符,货币符号等等?
  • 西里尔文,印度文,泰文和其他文字怎么样?
  • 中文,日文,韩文和其他表意字符集如何?

好的,那怎么解决?您如何处理?

正确的方法是使用Character类提供的方法。像这样:

if (Character.isLetter(...)) {
    if (/* character is upper or lower case a,e,i,o,u */) {
        ...
    } else {
        ...
    }
} else {
    ... 
}

或者,如果您想将分类限制为英文字母(即A到Z,a到z,无重音等),那么一种简单的方法是:

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' ||) {
    ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' ||
    ch == 'u' || ch == 'U') {
    ...
} else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
    ...
} else {
    ...
}