Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将一个字符与多个字符进行比较 for(int j=0;j_Java_Arrays_If Statement_Char_Boolean_Java9 - Fatal编程技术网

Java 将一个字符与多个字符进行比较 for(int j=0;j

Java 将一个字符与多个字符进行比较 for(int j=0;j,java,arrays,if-statement,char,boolean,java9,Java,Arrays,If Statement,Char,Boolean,Java9,这不是您想要的。请使用堆栈开关语句: for(int j=0 ; j<str.length() ; j++) { if(char[j]==(a||e||i||o||u)) count++; } 这段代码中有一个错误,稍后修复,我建议使用String.contains()并将每个搜索到的字符添加到字符串中 Matcher matcher = Pattern.compile("[aeiou]").matcher(str); while(matcher.find())

这不是您想要的。请使用堆栈
开关
语句:

for(int j=0 ; j<str.length() ; j++) {
    if(char[j]==(a||e||i||o||u))
        count++;
}

这段代码中有一个错误,稍后修复,

我建议使用
String.contains()
并将每个搜索到的字符添加到字符串中

Matcher matcher = Pattern.compile("[aeiou]").matcher(str);
while(matcher.find())
    count++;
private static final String SEARCH=“aeiou”;
公共静态void main(字符串[]args){
char[]chars=新字符[]{'a','b','a'};
整数计数=0;
for(int i=0;i
输出为2。(代码肯定可以优化)

这有三个好处:

  • 添加要查找的新角色要容易得多

  • 此代码不区分大小写。(请删除
    toLowerCase()
    方法调用以使其区分大小写)

  • 避免使用“长”if/else或switch/case块


  • 如果使用这些类,可以尝试使用正则表达式或简单字符串

    private static final String SEARCH = "aeiou";
    
    public static void main(String[] args) {
        char[] chars = new char[]{'a', 'b', 'A'};
        int count = 0;
    
        for (int i = 0; i < chars.length; i++) {
            if (SEARCH.contains((chars[i] + "").toLowerCase())) {
                count++;
            }
        }
    
        System.out.println(count);
    }
    
    String s=“aeiouaeiou”//要计数的字符串
    整数计数=0;
    对于(int i=0;i=0){
    计数++;
    }
    //另一个
    if(Character.toString(s.charAt(i))匹配(“[aeiou]”){
    计数++;
    }
    }
    
    聪明的regex部门还有一个:

    String s = "aeiouaeiou";//string to count
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
    
      //One method
      if ("aeiou".indexOf( s.charAt(i) ) >= 0) {
        count++;
      }
    
      //Another one
      if (Character.toString( s.charAt(i) ).matches("[aeiou]")) {
        count++;
      }
    
    }
    

    如果我真的需要使用
    char[]
    array而不是
    String
    实例,我总是使用
    Character
    类和正则表达式。如果你不知道正则表达式是什么,你应该学习它们,因为它们在处理字符串时非常有用。另外,你可以在上练习

    对于您的示例,我将使用以下内容:

    count = str.replaceAll("[^aeiou]","").length();
    
    char[]data=“Java编程很有趣”;
    int计数器=0;
    对于(int i=0;iJava 8,还有一个:

    char[] data = "programming in Java is fun".toCharArray();
    int counter = 0;
    
    for(int i = 0; i<data.length; i++){
        if(Character.toString(data[i]).matches("[aeiou]")){
            counter++;
        }
    }
    
    System.out.println(counter); // writes: 8
    

    使用
    字符
    列表
    和中的一种方法可以是:

    count = str.chars()
               .filter(c -> c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
               .count();
    
    List元音=List.of('a','e','i','o','u');//pverloaded便利工厂方法
    对于(int j=0;j
    使用
    String.contains()
    @Unihedron是的,如果
    count
    在发问者的代码之前可能是非零的,并且发问者的意图是在已经存在的代码中添加,
    +=
    是正确的。这是更快的方法。顺便说一句,这个问题有点小。@Unihedron老实说,我不确定
    switch
    语句是否在没有明确代码的情况下跳到了结尾
    default:
    case如果找不到匹配的case-作为一个
    开关
    仍然只不过是一个有组织的
    goto
    。如果是这样的话,那么我猜它没有修复错误,但至少我将它缩进了:D
    count = str.chars()
               .filter(c -> c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
               .count();
    
    List<Character> vowels = List.of('a','e','i','o','u'); // pverloaded convenience factory method 
    for(int j=0 ; j < str.length() ; j++) {
        if(vowels.contains(char[j])) {
            count++;
        }
    }