首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何计算Java字符串中的辅音数?

如何计算Java字符串中的辅音数?
EN

Stack Overflow用户
提问于 2014-08-12 10:42:15
回答 7查看 13.1K关注 0票数 0

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

代码语言:javascript
运行
复制
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);
  }   
}
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2014-08-12 10:50:24

这个很管用。我还改进了它,请参阅vowels.indexOf (而不是手动迭代)和isLetter (纠正元音错误检查)和输出(添加了;)和i++的行。

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2016-04-22 05:16:16

--这是最后的解决方案,伙计们:)

代码语言:javascript
运行
复制
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);
    }
}
票数 1
EN

Stack Overflow用户

发布于 2014-08-12 10:49:20

在这一行:else if(Character.isLetter(currentChar) && (vowels.charAt(index) == (currentChar))){,您正在检查您的字符是否为字母,以及是否为元音。您可以在条件测试中这样做:

代码语言:javascript
运行
复制
else if(Character.isLetter(currentChar)){
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25262270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档