-2

如何扩展此代码。我已经可以找到所有的元音了。

我需要添加它以打印辅音、单词、空格和特殊字符的数量。

import java.lang.String;
import java.io.*;
import java.util.*;

 public class CountVowels
 {

public static void main(String args[])throws IOException
{
       BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)           );
       System.out.println("Enter the String:"                                            );
       String text = bf.readLine(                                                        );
       int count = 0;
       for (int i = 0; i < text.length(); i++) {
       char c = text.charAt(i);
       if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
      {
       count++;
      }
      }
       System.out.println("There are" + " " + count + " " + "vowels"                      );
}
}

这是我到目前为止的代码。

编辑:当我指的是特殊字符时,我指的是 Shift + 1-0。

4

2 回答 2

1

你犯了一个典型的菜鸟错误:你在 main 方法中加入了太多东西。

创建一个接收字符串并计算元音的方法,另一个用于辅音,另一个用于空格、特殊字符、单词等。

这是一种方法。接口是可选的;将其视为一种学习经历。

public interface TextCounter {
    int countVowels(String s);
    // etc. - you get the idea.
}

现在你的实现:

public class TextCounterImpl implements TextCounter {
    public int countVowels(String s) {
        int numVowels = 0;
        if ((s != null) && (s.trim().length() > 0) {
            s = s.toLowerCase();
            for (int i = 0; i < s.length(); ++i) {
                char c = s.charAt(i);
                // What about the "sometimes 'y'" rule?
                if ((c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c == 'u')) {
                    ++numVowels;
                }
            }
        }
        return numVowels;
    }
}
于 2012-07-08T01:35:47.797 回答
0

您可以编写一个名为 LineParser 的类,它将解析字符串。使用 get 和 set 方法访问变量始终是一个好习惯。

class LineParser {

    public int numberOfVowels;
    public int numberOfConsonents;
    public int numberOfSpaces;
    public int numberOfSpecialChars;
    public int numberOfWords;
    public int numberOfDigits;

    public LineParser(String str)   {
       ProcessStr(str);
    }

    //Parses a string to count number of vowels, consonents, spaces, 
    //special characters and words,
    //Treates non letter chars as word terminators.
    //So London2Paris contains 2 words. 
    private void ProcessStr(String str) {

        boolean wordend = false;
        char[] word = new char[str.length()];
        int wordIndex = 0;

        for (char c : str.toCharArray()){           
            if (Character.isLetterOrDigit(c)){
                if (Character.isLetter(c)) {
                        word[wordIndex++] = c;
                        c = Character.toLowerCase(c);
                        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){
                           numberOfVowels ++;
                        }
                        else {
                           numberOfConsonents ++;
                        }
                 }
                 else {
                    numberOfDigits++;
                    wordend = true;
                 }
            } 
            else if (Character.isWhitespace(c)) {
                numberOfSpaces ++;
                wordend = true;
             } 
             else {             
                 numberOfSpecialChars ++;
                         wordend = true;
             }

             if (wordend == true && wordIndex > 0){
                 numberOfWords ++;
                 wordIndex = 0;
                 wordend = false;
             }
        }       
        wordend = true; 
        if (wordend == true && wordIndex > 0){
            numberOfWords ++;
        }
    }
}

此类可以像这样从您的主要功能中使用

public static void main(String args[])throws IOException
{
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the String:");
    String text = bf.readLine();
    LineParser parser = new LineParser(text);

    System.out.println("There are" + " " + parser.numberOfVowels + " " + "vowels");
    System.out.println("There are" + " " + parser.numberOfConsonents + " " + "Consonents");
    System.out.println("There are" + " " + parser.numberOfSpaces + " " + "Spaces");
    System.out.println("There are" + " " + parser.numberOfSpecialChars + " " + "SpecialChars");
    System.out.println("There are" + " " + parser.numberOfWords + " " + "Words");
}
于 2012-07-08T03:45:53.423 回答