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

Java 有没有更好的方法来编写与我相同的代码?,java,if-statement,Java,If Statement,我想写这段代码作为一个元音查找器,排除数字和符号或符号,除了逐个添加它们,还有更好的方法吗?代码运行良好,但我认为有更好的方法来实现这一点。我是java新手,两周前刚学过 import java.util.Scanner; public class ConsonantOrVowel { public static void main(String[] args) { char c; System.out.println("Please Input a Ch

我想写这段代码作为一个元音查找器,排除数字和符号或符号,除了逐个添加它们,还有更好的方法吗?代码运行良好,但我认为有更好的方法来实现这一点。我是java新手,两周前刚学过

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");
    }
}


您可以使用
String.inxedOf(charc)


您可以使用
String.inxedOf(charc)

普通旧阵列 您可以从使用一些数组开始

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");
}
正则表达式 这个有点复杂,但功能非常强大(正则表达式是您应该花时间学习的东西)

也许还有其他我从未想过的方式。。。 正如我所展示的,解决这个问题的方法不止一种。您使用哪一个将在很大程度上取决于您试图实现的目标、可用的输入和所需的输出 您可以从使用一些数组开始

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");
}
正则表达式 这个有点复杂,但功能非常强大(正则表达式是您应该花时间学习的东西)

也许还有其他我从未想过的方式。。。
正如我所展示的,解决这个问题的方法不止一种。您使用哪一个将在很大程度上取决于您试图实现的目标、可用的输入和所需的输出。在我看来,代码的真正问题在于这是不正确的

    else
        System.out.println("Input Character is a Consonant");
这是一个根本错误的假设。假设前两个集合中不包含的所有字符都必须是辅音

  • SP(空格)、HT(制表符)和其他各种控制字符呢
  • 拉丁1重音字符、货币符号等呢
  • 西里尔文、印度文、泰文和其他文字呢
  • 中文、日文、韩文和其他表意字符集呢
好的,那么解决方案是什么?你是怎么处理的

正确的方法是使用
字符
类提供的方法。大概是这样的:

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 {
    ...
}
if(ch='a'| | ch='a'| | ch='e'| | ch='e'|){
ch='i'| | ch='i'| | ch='o'| ch='o'||
ch=='u'| | ch=='u'){
...

}否则,如果(ch>='a'&&ch='a'&&chIMO,代码的真正问题在于这是不正确的

    else
        System.out.println("Input Character is a Consonant");
这是一个根本不正确的假设。你假设前两个集合之外的所有字符都必须是辅音

  • SP(空格)、HT(制表符)和其他各种控制字符呢
  • 拉丁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 {
    ...
}
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='a'&&ch)我的第一个想法是使用一个或多个数组或
java.util.List
来存储您要查找(和/或要排除)的值。由于
java.util.List
具有
包含
,您只需执行一行检查,查看您的输入是否在包含或排除列表中。您可以创建元音和辅音的
模式,然后将您的输入与模式匹配。您知道类型转换吗?对于像这样的简单输入,您可以转换字符
c
转换为其各自的int值,并检查您的int值是否在特定范围内。例如,如果您将char
H
转换为int,则会得到
72
。但这仅在ascii范围内有效。对于元音,您可以检查类似
“aeiou.”的内容(字母)
其中字母是长度为1的
字符串变量。条目无效:
如果(!Character.toString(c).matches((?i)[a-z]){
。条目是元音:
如果(Character.toString(c).matches((?i)[aeiou]){
。否则它是辅音。这个问题可能更适合(尽管先阅读他们的规则)。我的第一个想法是使用一个或多个数组或
java.util.List
来存储要查找(和/或要排除)的值。由于
java.util.List
具有
包含
,您只需执行一行检查,查看您的输入是否在包含或排除列表中。您可以创建元音和辅音的
模式,然后将您的输入与模式匹配。您知道类型转换吗?对于像这样的简单输入,您可以转换字符
c
转换为相应的int值,并检查int值是否在特定范围内。例如,如果您转换字符
H