Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.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中,如何创建一个简单的程序来打印短语中辅音和元音的数量?_Java - Fatal编程技术网

在Java中,如何创建一个简单的程序来打印短语中辅音和元音的数量?

在Java中,如何创建一个简单的程序来打印短语中辅音和元音的数量?,java,Java,以下是我到目前为止的情况: System.out.println("CONSONANT AND VOWEL COUNTER: Please type a phrase: "); String lastPhrase = keyboard.nextLine(); int countCon = 0; int countVow = 0; if (lastPhrase.contains("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWX

以下是我到目前为止的情况:

 System.out.println("CONSONANT AND VOWEL COUNTER: Please type a phrase: ");
    String lastPhrase = keyboard.nextLine();

    int countCon = 0;
    int countVow = 0;

    if (lastPhrase.contains("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ")) {
        countVow++;
    }
    if (lastPhrase.contains("abcdefghijklmnopqrstuvwxyzABCDEFGHJKLIMNOPQRSTUVWXYZ")) {
        countCon++;
    }
    System.out.println("There are " + countVow + " vowels and " + countCon + " consonants.");

这两个值都为0。有什么问题?

包含搜索整个字符串,而不是单个字母

最简单的方法是手动检查每个字符,从我的头顶开始,假设我没有丢失任何神奇的字符串方法

您应该使用
toUpperCase
将整个字符串转换为大写,然后检查字符是否为元音AEIOU

if(string.charAt(i) == 'A' || ... /* and so on */) {
    countVow++;
}
else {
    countCons++;
}
如果是,在元音上加1。否则,辅音加1。它不是元音就是辅音,所以如果你只检查这五个字符,你就知道它是什么了


因为这可能是一个家庭作业问题,所以我为您提供了一个正确方向的步骤。您应该朝着解决方案努力,如果需要帮助,请回来。

根据Java文档

字符串包含(字符序列) 当且仅当此字符串包含指定的字符值序列时,返回true

计算元音数量的最简单方法是循环检查字符串对象的每个字符

String s = "Whatever you want it to be.".toLowercase();
int vowelCount = 0;
for (int i = 0, i < s.length(); ++i) {
    switch(s.charAt(i)) {
        case 'a':
            vowelCount++;
            break;
        case 'e':
            vowelCount++;
            break;
        case 'i':
            vowelCount++;
            break;
        case 'o':
            vowelCount++;
            break;
        case 'u':
            vowelCount++;
            break;
        default:
            // do nothing
    }
}
String s=“不管你想要什么。”.toLowercase();
int-vouelcount=0;
对于(int i=0,i
我会这样做:

//convert string to lowercase
//for loop looping over the string    
if(string.charAt(i).matches([aeiou]) {
        countVow++;
}
else if(isLetter(string.charAt(I))){
        countCons++;  
} //end for loop
看看和。

类似的东西

String元音=“aeuyio”;
字符串辅音=“bcdfgh…”
字符串短语=“amsdasmdnsn”;
int元音计数=0,辅音计数=0;
for(char ch:phrase.toCharArray()){
if(元音.contains(String.valueOf(ch))){
++元音计数;
}
if(辅音.contains(String.valueOf(ch))){
++辅音;
}
}

String.contains仅适用于字符串和正则表达式。我能想到的计算所有辅音的最快方法是:

String onlyConsonants = lastPhrase.replaceAll("[\\saeiouAEIOU0-9]", "");
countCon = onlyConsonants.length();
String onlyVowels = lastPhrase.replaceAll("[^\\saeiouAEIOU0-9]", "");
countVow = onlyVowels.length();

我认为这解决了您的问题。

您正在询问
lastphase
是否包含这些长字符串。是吗?您需要
循环
charAt()
。最好使用String.matches()和正则表达式来表示元音和辅音。也没那么复杂,我同意。解决这个问题有多种方法。String.matches()是一种更优雅的方法,但Zander必须首先给出正则表达式。如果字符串有数字或标点符号呢?那么它就不起作用了。@reneéG那么你可以在
else
中使用
isleter()
作为
else if
。我试过了,但它说的是第一类布尔值第二类字符。我该如何解决这个问题?