如何计算Java中String中辅音的数量?

时间:2014-08-12 10:42:16

标签: java

我正在编写一个程序,用于计算用户输入的句子中的元音和辅音数量。我的下面的代码计算元音的数量,但它给出了辅音计数的奇怪数字。例如,如果我输入" g"我的辅音数为10。

import java.util.Scanner;

public class VowelCount{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter a sentence :");
    String sentence = scan.nextLine();

    String vowels = "aeiouAEIOU";
    int vowelCount = 0;
    int consCount = 0;
    int i;

      for(i = 0; i < sentence.length(); i += 1){
        char currentChar = sentence.charAt(i);
        int index;
        for(index = 0; index < vowels.length(); index += 1){
          if(vowels.charAt(index) == (currentChar)){
            vowelCount++;
          }else if(Character.isLetter(currentChar) && (vowels.charAt(index) == (currentChar))){
            consCount++;
          }
        }
      }
      System.out.println(consCount);
      System.out.println(vowelCount);
  }   
}

7 个答案:

答案 0 :(得分:1)

这很有效。我也对其进行了改进,请参阅vowels.indexOf(而不是手动迭代)和isLetter(校正错误的元音检查)和输出(添加;)和i++的行

public class VowelCount{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter a sentence :");
    String sentence = scan.nextLine();

    String vowels = "aeiouAEIOU";
    int vowelCount = 0;
    int consCount = 0;
    int i;

    int length = sentence.length();
    for(i = 0; i < length; i ++){
        char currentChar = sentence.charAt(i);
        if (vowels.indexOf(currentChar)>=0)
           vowelCount++;
        else if(Character.isLetter(currentChar))
                consCount++;
    }
    System.out.print(consCount+";");
    System.out.print(vowelCount);
  }
}

答案 1 :(得分:1)

这是最终的解决方案:)

import java.util.Scanner;

public class VowelConsCount
{ public static void main(String args[])
    { System.out.print("Enter a sentence :");
      Scanner in = new Scanner(System.in);
      String sentence = in.nextLine();

      String vowels = "aeiouAEIOU";
      int vowelCount = 0;
      int consCount = 0;
      int i;

      for(i = 0; i < sentence.length(); i ++)
         { char currentChar = sentence.charAt(i);
           if (vowels.indexOf(currentChar)>=0)
                vowelCount++;
           else if(Character.isLetter(currentChar))
                consCount++;
         }
      System.out.print("\nNumber of vowels is "+vowelCount);
      System.out.print("\nNumber of consonant is "+consCount);
    }
}

答案 2 :(得分:0)

在那一行: else if(Character.isLetter(currentChar) && (vowels.charAt(index) == (currentChar))){ 你正在检查你的字母是否是一封信,以及它是否是一个元音。您可以在条件测试中尝试:

else if(Character.isLetter(currentChar)){

答案 3 :(得分:0)

问题在于你的内部for循环的其他部分。

boolean isVowel(char c){
   String vowels = "aeiouAEIOU";
   for(int index = 0; index < vowels.length(); index += 1)
   if(vowels.charAt(index) == c)
      return true;
   return false;
}

现在,在你获得currentChar后,试试这个

if(isVowel(currentChar))
   vowelCount++;
else
   constCount++;

答案 4 :(得分:0)

您的代码有几个问题:

  1. 您使用System.out.print代替System.out.println,这会使阅读号码误导。
  2. 如果找到元音,您正在循环可能的元音并递增vowelCount但是如果没有,则递增consCount!。你应该分开两个:先检查元音,然后检查辅音。

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter a sentence :");
    String sentence = scan.nextLine();
    
    String vowels = "aeiouAEIOU";
    int vowelCount = 0;
    int consCount = 0;
    int i;
    System.out.println("["+sentence+"]");
    for (i = 0; i < sentence.length(); i += 1)
    {
        char currentChar = sentence.charAt(i);
        boolean isVowel = false;
        int index;
        for (index = 0; index < vowels.length(); index += 1)
        {
            if (vowels.charAt(index) == (currentChar))
            {
                vowelCount++;
                isVowel =true;
                break;//we found the vowel, no need to keep looping
            }
        }
    
         if (!isVowel && Character.isLetter(currentChar))//vowel have not been found
        {
            consCount++;
        }
    
    }
    
    System.out.println("cons: " + consCount);
    System.out.println("vowel: " + vowelCount);
    

答案 5 :(得分:0)

使用此:

Scanner scan = new Scanner(System.in);
    System.out.print("Enter a sentence :");
    String sentence = scan.nextLine();

    String vowels = "aeiouAEIOU";
    int vowelCount = 0;
    int consCount = 0;
    int i;
    boolean flag;
      for(i = 0; i < sentence.length(); i += 1){
        char currentChar = sentence.charAt(i);
        flag=false;
        int index;
        for(index = 0; index < vowels.length(); index += 1){
          if(vowels.charAt(index) == (currentChar)){
            vowelCount++;
            flag=true;
            continue;
          }
        }
        if(!flag && Character.isLetter(currentChar) ){
            consCount++;
         }
      }
      System.out.println(consCount);
      System.out.println(vowelCount);

<强>更改

你在做什么:

 for(index = 0; index < vowels.length(); index += 1){
  if(vowels.charAt(index) == (currentChar)){
      vowelCount++;
    }else if(Character.isLetter(currentChar) && (vowels.charAt(index) == (currentChar))){
        consCount++;
  }

每当consCount与元音不匹配时,hello就会增加。 例如,如果您尝试consCounth仅会consCount++增加到10,因为它不会对任何元音进行数学运算。

所以我在这里做的是将flag放在第二个for循环之外并添加一个consCount++来检查它是否是元音。如果它是一个元音,那么{{1}}将被跳过,如果没有,那么它将被检查为后者被认为是一个辅音。

答案 6 :(得分:0)

public static void main(String[] args) {

    Scanner stringin = new Scanner(System.in);
    String string1;
    System.out.println("Enter a string: ");
    string1 = stringin.nextLine();
    string1 = string1.toLowerCase();

    int count = 0;
    int vowels = 0;
    int consonants = 0;
    int spaces = 0;
    for (int i = 0; i < string1.length(); i++) {

        char letter = string1.charAt(i); 
        if (letter == 'a' || letter == 'e' || 
                letter == 'i' || letter == 'o' ||
                    letter == 'u')
        {
            vowels++;
        }
        else if  (letter == ' ')
        {
            spaces++;
        }
        else
        {
            consonants++;


        }
    }
    System.out.print("The number of vowels is " + vowels);
    System.out.print(" The number of consonants is " + consonants);
}

这是我的解决方案。我不得不对空间进行例外处理,因为它被视为辅音。目前正在上Java课程,所以我根本没有经验。