java Java计算元音程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28381626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:29:12  来源:igfitidea点击:

Java count the vowels program

java

提问by Johnnymul346

I'm trying to make a program that lets the user input a word, then it will count the number of vowels and consonants in that word and print the amount. e.g: The word 'YOURWORD' contains 3 vowels and 5 consonants.

我正在尝试制作一个程序,让用户输入一个单词,然后它会计算该单词中元音和辅音的数量并打印数量。例如:“YOURWORD”这个词包含 3 个元音和 5 个辅音。

Since a consonant is just each letter that isn't a vowel, I only made the program check if there was a vowel in the word and then the number of consonants is just the number of other letters in the word. However, I'm struggling with the for loop. Here is my code:

由于辅音只是每个不是元音的字母,我只让程序检查单词中是否有元音,然后辅音的数量就是单词中其他字母的数量。但是,我正在为 for 循环而苦苦挣扎。这是我的代码:

   String word;
    Scanner myinput = new Scanner(System.in);

    System.out.println("Please enter a word.");
    word = myinput.next();
    char[] wordc = word.toCharArray();

    for(int w = 0;w > word.length();w++;) {
      if(wordc[w] == 'a' || wordc[w] == 'e' || wordc[w] == 'i' || wordc[w] == 'o' || wordc[w] == 'u') {

      }

As you can see, I'm so close to the end but I literally have no clue what to do now. I'm a beginner at Java and I have checked the for loop syntax but I really don't know what to do, please help.

正如你所看到的,我已经接近尾声了,但我真的不知道现在该做什么。我是 Java 的初学者,我已经检查了 for 循环语法,但我真的不知道该怎么做,请帮忙。

回答by shauryachats

Try changing

尝试改变

for(int w = 0;w > word.length();w++)

to

for(int w = 0;w < word.length();w++)

because when you set wto 0, obviously, your word.length()is greater than 0, which makes the condition in the forloop false. Hence, the forloop would be notbe executed even once.

因为当您设置w为 0 时,显然您word.length()的值大于 0,这使得for循环中的条件为假。因此,即使一次for不会执行循环。

To count the number of vowels, you define one more integer outside the forloop and increment it as you encounter a vowel. So, your code should look like:

要计算元音的数量,您可以在for循环外再定义一个整数,并在遇到元音时将其递增。因此,您的代码应如下所示:

String word;
Scanner myinput = new Scanner(System.in);
System.out.println("Please enter a word.");
word = myinput.next();
//Convert your string to lowercase using toLowerCase() method.
char[] wordc = word.toLowerCase().toCharArray();
int vowels = 0; //This counter will count the vowels.
for(int w = 0;w < word.length();w++) {
  if(wordc[w] == 'a' || wordc[w] == 'e' || wordc[w] == 'i' || wordc[w] == 'o' || wordc[w] == 'u') {
      vowels++;
  }
}
int consonants = word.length() - vowels; 
//Assuming no special characters in your word.

回答by Ananth

String word;
Scanner myinput = new Scanner(System.in);

System.out.println("Please enter a word.");
word = myinput.next();
char[] wordc = word.toCharArray();

int vowels = 0;

for (char w: wordc) {
    if(w == 'a' || w == 'e' || w == 'i' || w == 'o' || w == 'u') {
        vowels++;
    }
}

System.out.println("Number of vowels in " + word + " is: " + vowels);
System.out.println("Number of consonants in " + word + " is: " + (wordc.length - vowels));

回答by anubhava

Here is a way to count vowels and consonants without using loops and multiple conditions.

这是一种在不使用循环和多个条件的情况下计算元音和辅音的方法。

String word = "YOURWORD"; // assuming only letters

String vowels = word.replaceAll( "(?i)[^aeiou]+", "" ); // OUO

int numVowels     = vowels.length(); // 3
int numConsonants = word.length() - numVowels; // 5

How it works:

怎么运行的:

In the word.replaceAll( "(?i)[^aeiou]+", "" )call we are replacing everything that is not one of [^aeiou]thus leaving only vowels in returned string.

word.replaceAll( "(?i)[^aeiou]+", "" )调用中,我们将替换所有不属于其中的所有内容,[^aeiou]因此只在返回的字符串中留下元音。

回答by finxxi

You should define two int variables to do counting. the for loop goes like below

您应该定义两个 int 变量来进行计数。for 循环如下所示

   String word;
int numberOfVowls = 0; numberofCosonents = 0;
    Scanner myinput = new Scanner(System.in);

    System.out.println("Please enter a word.");
    word = myinput.next();
    char[] wordc = word.toCharArray();

    for(int w = 0;w < word.length();w++;) {
      if(wordc[w] == 'a' || wordc[w] == 'e' || wordc[w] == 'i' || wordc[w] == 'o' || wordc[w] == 'u') numberOfVowls++;
else numberofCosonents++;}

回答by Johnnymul346

String[] arr = text.split(" ");
     char elementch = ' ';

     //looping through string array
    for(int i=1; i<= arr.length ; i++){
        String nextElement = arr[i-1];

        int add = 0;
            //looping through each character in the next element
            for (char ch: nextElement.toCharArray()) {
                //checking if ch == to vowels
                if(ch == 'e'|| ch == 'a' || ch == 'o' || ch == 'u'|| ch == 'i'){
                    //add counts number of vowels for every string array index
                    add = add +1;
                    elementch = ch; 
                    System.out.print(" ' " +elementch + " ' ");
                }else{
                    //do nothing
                }
            }
            System.out.println("The number of vowels is "+ add + " in" + " : " + nextElement.toUpperCase());
    }