Java计算元音程序

时间:2015-02-07 11:34:37

标签: java

我试图制作一个允许用户输入单词的程序,然后它将计算该单词中的元音和辅音的数量并打印该数量。例如:“你自己”这个词。包含3个元音和5个辅音。

由于辅音只是每个不是元音的字母,我只会检查程序中是否有单词中的元音,然后辅音的数量只是单词中其他字母的数量。但是,我在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') {

      }

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

5 个答案:

答案 0 :(得分:3)

尝试更改

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

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

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

要计算元音的数量,您需要在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.

答案 1 :(得分:0)

您应该定义两个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++;}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

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

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

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

工作原理:

word.replaceAll( "(?i)[^aeiou]+", "" )调用中,我们将替换不是[^aeiou]之一的所有内容,从而只返回返回字符串中的元音。

答案 4 :(得分:-1)

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