【问题标题】:Counting vowels with consonants用辅音计算元音
【发布时间】:2014-01-31 00:43:52
【问题描述】:

我编写了一个程序来计算输入字符串中元音和辅音的数量:

        Scanner in = new Scanner(System.in);


System.out.print("Enter a string ");
    String phrase = in.nextLine();

int i, length, vowels = 0;
int consonants = 0;
boolean y = false;
String j;
length = phrase.length();
for (i = 0; i < length; i++)
{



        j = "" + phrase.charAt(i);


     boolean isAVowel = "aeiou".contains(j.toLowerCase());
     boolean y = "y".contains(j.toLowerCase());


     if(isAVowel){
         vowels++;
     }else if(isAVowel && y){
     vowels++;
    consonants++;
//}else if(y && !(isAVowel)){
//   vowels++;
      }else{
      consonants++;
      }


System.out.println("The number of vowels in \"" +phrase+"\" is "+ vowels+".\n\nThe number of consonants is "+consonants+".\n\n");

当“y”本身是一个辅音时,它应该是一个元音。我在哪里说明这一点?

【问题讨论】:

  • btw:不用在循环中使用 substring(i, i+1) ,您可以简单地使用 phrase.charAt(i) 来遍历字符串中的所有字符。这种方法提高了效率和可读性。
  • 如:j = phrase.substring(i); ?
  • 其实更像:j = "" + phrase.charAt(i);

标签: java string count


【解决方案1】:

这里发生了几件事:

  1. j.equalsIgnoreCase("a,e,i,o,u") 将检查 j(长度为 1 的字符串)是否为字符串 "a,e,i,o,u",这几乎当然不是你想要的(因为它总是错误的,因此你为每个辅音设置 y=true)。相反,请考虑在每次迭代开始时将布尔值设置为 false,并在元音分支中将其设置为 true。然后,如果该变量为真,您就知道这次您看到了一个元音。或者只使用 else 分支。

  2. 您在循环外将 y 初始化为 false,但一旦 y 为 true,它就永远不会被重置,因此对于每个字母,您将运行 if (y == true) 块。

  3. 目前,您的系统仅考虑具有 1 个 y 且没有元音的单词。如果输入“yyy”,则会得到 1 个元音和 2 个辅音。

在风格上,您可以进行许多其他更改,以使您的程序更易于阅读和调试。以下是一些:

检查布尔值时不必执行“== true”。例如,不要使用“if (y == true)”,而是使用“if (y)”。

所有元音的处理方式都相同,因此您不需要为每个元音单独分配分支。例如,您可以:

if (j.equalsIgnoreCase("a") 
    || j.equalsIgnoreCase("e")
    || j.equalsIgnoreCase("i")
    || ...)
{
    vowels++;
}

事实上,您可以通过检查元音值的集合、使用正则表达式或在本例中简单地使用 contains 来进一步简化此操作:

boolean isAVowel = "aeiou".contains(j.toLowerCase());

考虑单独计算 y,因此您可以在 3 个单独的计数器中跟踪元音、ys 和常量。然后,最后你可以决定是在元音还是辅音上加 ys。

最后,在调试阶段将System.out.println("vowels = " + vowels + ", consonants = " + consonants + "...") 添加到循环内部。这将使您更容易了解正在发生的事情以及事情开始出错的地方。

【讨论】:

  • 错字? which is almost certainly what you want
【解决方案2】:

也许你应该简单地使用正则表达式

String phrase = in.nextLine();
int consonants = phrase.replaceAll("a|e|o|u|i", "").length();
int vowels = phrase.replaceAll("[^a|e|o|u|i|y]", "").length();

【讨论】:

    【解决方案3】:

    我无法理解你到底想用 'y' 做什么,所以分别计算了它们。 需要先从输入中删除所有非单词字符

    我已经修改了你的代码(虽然没有优化):

    System.out.print("Enter a string: ");
    String origphrase = new Scanner(System.in).nextLine();
    String phrase = origphrase.replaceAll("\\W","");
    int i, length, vowels = 0;
    int consonants = 0;
    int ys=0;
    String j;
    
    length = phrase.length();
    for (i = 0; i < length; i++)
    {
      j = "" + phrase.charAt(i);
      boolean isAVowel = "aeiou".contains(j.toLowerCase());
      boolean y = "y".contains(j.toLowerCase());
      if(isAVowel){
        vowels++;
      }else if(y){
        ys++;
      }else{
        consonants++;
      }
    }
    
    System.out.println("Phrase:"+origphrase);
    System.out.println("Vowels:"+vowels);
    System.out.println("Consonants:"+consonants);
    System.out.println("Y's:"+ys);
    

    【讨论】:

    • ys 添加到vowelsconsonants 您想要的位置
    【解决方案4】:

    以下递归函数返回输入字符串中元音的个数

    public static int vc(String s){
        if(s.length() - 1 < 0) return 0;
        return ((("aeiou".indexOf((s.charAt(s.length()-1)+"").toLowerCase()) >= 0 ? 1 : 0))  
        + vc((s = s.substring(0,s.length()-1))));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-07
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多