Java 检查字符串是否包含字符数组中的值

Java 检查字符串是否包含字符数组中的值,java,python-3.x,Java,Python 3.x,我已经有一段时间没有用java编程了,今天我试图刷新我的记忆,了解如何使用它来做事情。(因为我正在为java面试做准备)我试图在包含一个句子的字符串中查找元音,并返回找到的元音数量 在python中,我是这样做的 vowels = ('a', 'e', 'i', 'o', 'u') #tuple containing vowels sentence = "Refreshing my memory on java" vowels_count = 0; consonant_count = 0; fo

我已经有一段时间没有用java编程了,今天我试图刷新我的记忆,了解如何使用它来做事情。(因为我正在为java面试做准备)我试图在包含一个句子的字符串中查找元音,并返回找到的元音数量

在python中,我是这样做的

vowels = ('a', 'e', 'i', 'o', 'u') #tuple containing vowels
sentence = "Refreshing my memory on java"
vowels_count = 0;
consonant_count = 0;
for s in sentence:
    if s in vowels:
        vowels_count += 1
    else:
        if s.isalpha():
            consonant_count += 1

print(vowels_count)
print(consonant_count)
我试着用java做一些类似的事情,但不知为什么我的辅音计数总是错误的。我怎样才能修好它

String sentence2 = "Refreshing my memory on java";
char[] lookout = {'a', 'e', 'i', 'o', 'u'};
int vowels = 0;
int consonants = 0;
for (int i=0; i < sentence2.length(); i++) {
        for (int j = 0; j < lookout.length; j++) {
            if (sentence2.charAt(i) == lookout[j]) {
                vowels++;
            } else {
                if (Character.isLetter(sentence2.charAt(j))) {
                    consonants++;
                }
            }
        }
    }

System.out.print(String.format("vowels count is %d consonant count is %d \n", vowels, consonants));
String sentence2=“刷新java上的内存”;
char[]lookout={'a','e','i','o','u'};
int元音=0;
int辅音=0;
for(int i=0;i
Character.isleter(句子2.charAt(j))

以上必须是

Character.isleter(句子2.charAt(i))

(j只能在0到4之间变化。因此您一直从字符串中选取前四个字符)

还有,
y
什么时候变成元音;)

但是你不能在for循环中这样做,因为你会把一个辅音数到5次

您可以使用
标志
来指示是否找到元音。如果不是,则增加辅音计数

for (int i=0; i < sentence2.length(); i++) {
    boolean vowel = false;
    for (int j = 0; j < lookout.length; j++) {
        if (sentence2.charAt(i) == lookout[j]) {
            vowel = true;
            vowels++;
            break; //A vowel is found
        }
    }
    if (!vowel && Character.isLetter(sentence2.charAt(i))) {
        consonants++;
    }
}
for(int i=0;i

更好的方法是使用列表或集合,并使用中的
contains
方法。

我认为最简单的方法是创建一组元音字符,这样您就可以轻松地查询字符是否为元音

类似的东西应该可以工作(我还没有测试过,所以可能会有小错误)

final Set元音_Set=Set.of('a','e','i','o','u','y');
int元音=0;
int辅音=0;
for(int i=0;i
当你说

} else {
    if (Character.isLetter(sentence2.charAt(j))) {
       consonants++;
   }

基本上,你只是把每个字母都算作一个辅音,你所要做的就是改变,只说else辅音+++

首先,你没有考虑大写字母。
第二,第2[j]句是什么??它不应该是else块中的第2[i]句吗?这就是问题所在

使用任何已经提供的答案(我会说答案就是我自己所做的)。要避免计算空格,并考虑大写元音,可以执行以下操作:

String fixedStr = sentence2.replace(" ", "").trim().toLowerCase();
然后在执行
字符(int)
检查时使用
fixedStr

然而,使用答案,它将是

String sentence2 = "Refreshing my memory on java";
String fixedStr = sentence2.replace(" ", "").trim().toLowerCase();

Set<Character> lookout = new TreeSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));

int vowels = 0;
int consonants = 0;

for(int i = 0; i < fixedStr.length(); i++) {
    if(lookout.contains(fixedStr.charAt(i))) vowels++;
    else consonants++;
}

System.out.print(String.format(
            "total characters to check is %d (%d with whitespace), vowels " +
            "count is %d, consonant count is %d, which adds up to %d \n",
            fixedStr.length(), sentence2.length(), vowels, consonants,
            vowels + consonants));
String sentence2=“刷新java上的内存”;
String fixedStr=sentence2.replace(“,”).trim().toLowerCase();
Set lookout=newtreeset(Arrays.asList('a','e','i','o','u');
int元音=0;
int辅音=0;
对于(int i=0;i

Set lookout=new TreeSet(…)
可替换为
Set lookout=TreeSet.of('a'…)在Java 9和更新版本中。

感谢@user7的回答和解释,我意识到了我的错误,并设法使我的算法工作

for (int i = 0; i < sentence2.length(); i++) {
        for (int j = 0; j < lookout.length; j++) {
            if (sentence2.charAt(i) == lookout[j]) {
                vowels ++;
            }
        }
        if (Character.isLetter(sentence2.charAt(i))) {
            consonants++;
        }
    }
//subtracted vowel count from consonants :D
System.out.print(String.format("vowels count is %d consonant count is %d \n", vowels, consonants-vowels));
for(int i=0;i
首先将源字符串转换为小写或大写。

/*Convert to lower case*/
sentence2 = sentence2.toLowerCase();
现在写一个检查元音是否正确的方法

/*Method for checking vowel or not*/
public static boolean isVowel(int ch) {
    if ("aeiou".indexOf(ch) < 0) {
        return false;
    } else {
        return true;
    }
}
Loop through your string and test the condition.

for (int i = 0; i < sentence2.length(); i++) {
    /*Check is character or not*/
    if (Character.isAlphabetic(sentence2.charAt(i))) {
        /*Check for vowel or not*/
        if (isVowel(sentence2.charAt(i))) {
            vowels++;
        } else {
            consonants++;
        }
    }

}
检查元音与否的方法*/ 公共静态布尔元音(int-ch){ 如果(“aeiou”。索引of(ch)<0){ 返回false; }否则{ 返回true; } } 循环遍历字符串并测试条件。 for(int i=0;i
所以整个答案是这样的

public static boolean isVowel(int ch) {
    if ("aeiou".indexOf(ch) < 0) {
        return false;
    } else {
        return true;
    }
}

public static void main(String...args) {
    String sentence2 = "Refreshing my memory on java";
    int vowels = 0;
    int consonants = 0;
    /*Convert to lower case*/
    sentence2 = sentence2.toLowerCase();

    for (int i = 0; i < sentence2.length(); i++) {
        /*Check is character or not*/
        if (Character.isAlphabetic(sentence2.charAt(i))) {
            /*Check for vowel or not*/
            if (isVowel(sentence2.charAt(i))) {
                vowels++;
            } else {
                consonants++;
            }
        }
    }
    System.out.print(String.format("vowels count is %d consonant count is %d \n", vowels, consonants));
}
publicstaticbooleanisvuel(int-ch){
如果(“aeiou”。索引of(ch)<0){
返回false;
}否则{
返回true;
}
}
公共静态void main(字符串…参数){
String sentence2=“在java上刷新我的内存”;
int元音=0;
int辅音=0;
/*转换成小写*/
句子2=句子2.toLowerCase();
for(int i=0;ipublic static boolean isVowel(int ch) {
    if ("aeiou".indexOf(ch) < 0) {
        return false;
    } else {
        return true;
    }
}

public static void main(String...args) {
    String sentence2 = "Refreshing my memory on java";
    int vowels = 0;
    int consonants = 0;
    /*Convert to lower case*/
    sentence2 = sentence2.toLowerCase();

    for (int i = 0; i < sentence2.length(); i++) {
        /*Check is character or not*/
        if (Character.isAlphabetic(sentence2.charAt(i))) {
            /*Check for vowel or not*/
            if (isVowel(sentence2.charAt(i))) {
                vowels++;
            } else {
                consonants++;
            }
        }
    }
    System.out.print(String.format("vowels count is %d consonant count is %d \n", vowels, consonants));
}