如何扩展此代码。我已经可以找到所有的元音。
我需要添加它以打印辅音,单词,空格和特殊字符的数量。
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。
答案 0 :(得分:1)
你犯了一个典型的新手错误:你在主要方法中投入太多东西。
创建一个方法,它接受一个String并计算元音,另一个用于辅音,另一个用于空格,特殊字符,单词等。
这是一种方法。界面是可选的;认为这是一种学习经历。
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;
}
}
答案 1 :(得分: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");
}