我已经可以计算元音,辅音,空格和特殊字符。 我必须使用一些初学者技术。 [编辑:鉴于最后一个声明和以下代码,这个问题听起来像家庭作业的帮助]没有什么太复杂。
import java.util.Scanner;
public class JBlahBlah
{
static Scanner sc = new Scanner(System.in );
public static void main(String[] Theory)
{
JWaffles MyWaffles = new JWaffles();
MyWaffles.ProgramHeading();
System.out.println("Enter a string:" );
String SentenceContents = sc.nextLine( );
int VowelCount = 0,ConsonantCount = 0,WordCount = 0,SpaceCount = 0,SpecialCharCount = 0 ;
for (int Bat = 0; Bat < SentenceContents.length(); Bat++)
{
char Vowels = SentenceContents.charAt(Bat );
if ( (Vowels == 'A') || (Vowels == 'a' )
|| (Vowels == 'E') || (Vowels == 'e' )
|| (Vowels == 'I') || (Vowels == 'i' )
|| (Vowels == 'O') || (Vowels == 'o' )
|| (Vowels == 'U') || (Vowels == 'u' ))
VowelCount++;
char Consonants = SentenceContents.charAt(Bat);
if ( (Consonants == 'B') || (Consonants == 'b')
|| (Consonants == 'C') || (Consonants == 'c')
|| (Consonants == 'D') || (Consonants == 'd')
|| (Consonants == 'F') || (Consonants == 'f')
|| (Consonants == 'G') || (Consonants == 'g')
|| (Consonants == 'H') || (Consonants == 'h')
|| (Consonants == 'J') || (Consonants == 'j')
|| (Consonants == 'K') || (Consonants == 'k')
|| (Consonants == 'L') || (Consonants == 'l')
|| (Consonants == 'M') || (Consonants == 'm')
|| (Consonants == 'N') || (Consonants == 'n')
|| (Consonants == 'P') || (Consonants == 'p')
|| (Consonants == 'Q') || (Consonants == 'q')
|| (Consonants == 'R') || (Consonants == 'r')
|| (Consonants == 'S') || (Consonants == 's')
|| (Consonants == 'T') || (Consonants == 't')
|| (Consonants == 'V') || (Consonants == 'v')
|| (Consonants == 'W') || (Consonants == 'w')
|| (Consonants == 'X') || (Consonants == 'x')
|| (Consonants == 'Y') || (Consonants == 'y')
|| (Consonants == 'Z') || (Consonants == 'z') )
ConsonantCount++;
char Spaces = SentenceContents.charAt(Bat);
if ( (Spaces == ' ') )
SpaceCount++;
char SpecialCharacters = SentenceContents.charAt(Bat);
if ( (SpecialCharacters == '!') || (SpecialCharacters == '@' )
|| (SpecialCharacters == '#') || (SpecialCharacters == '$' )
|| (SpecialCharacters == '%') || (SpecialCharacters == '^' )
|| (SpecialCharacters == '&') || (SpecialCharacters == '*' )
|| (SpecialCharacters == '(') || (SpecialCharacters == ')' )
|| (SpecialCharacters == '-') || (SpecialCharacters == '_' )
|| (SpecialCharacters == '+') || (SpecialCharacters == '=' )
|| (SpecialCharacters == ',') || (SpecialCharacters == '<' )
|| (SpecialCharacters == '.') || (SpecialCharacters == '>' )
|| (SpecialCharacters == '?') || (SpecialCharacters == '/' )
|| (SpecialCharacters == '"') || (SpecialCharacters == ';' )
|| (SpecialCharacters == ':') || (SpecialCharacters == '{' )
|| (SpecialCharacters == '[') || (SpecialCharacters == '}' )
|| (SpecialCharacters == ']') || (SpecialCharacters == '~' )
|| (SpecialCharacters == '`') || ((SpecialCharacters == '1' )
|| (SpecialCharacters == '2') || (SpecialCharacters == '3' )
|| (SpecialCharacters == '4') || (SpecialCharacters == '5' )
|| (SpecialCharacters == '6') || (SpecialCharacters == '7' )
|| (SpecialCharacters == '8') || (SpecialCharacters == '9' )
|| (SpecialCharacters == '|') ))
SpecialCharCount++;
}
System.out.println("There are " + VowelCount + " vowels in this sentance" );
System.out.println("There are " + ConsonantCount + " consonants in this sentance" );
System.out.println("There are " + SpaceCount + " spaces in this sentance" );
System.out.println("There are " + SpecialCharCount + " special characters in this sentance");
}
}
我怎样才能使程序打印出单词数量?如果它们之间有几个空格怎么办?
答案 0 :(得分:3)
以下是使用API计算每种字符类型的方法:
int spaceCount = sentence.length() - sentence.replaceAll(" ", "").length();
int vowelCount = sentence.length() - sentence.replaceAll("(?i)[aeiou]", "").length();
int consonantCount = sentence.length() - sentence.replaceAll("(?i)(?=[a-z])[^aeiou]", "").length();
int specialCount = sentence.length() - sentence.replaceAll("(?i)[^a-z ]", "").length();
int wordCount = sentence.trim().split("\\s+").length;
虽然你可能认为这不是“教你如何编程”,但它实际上是:学习一门语言的一部分是通过让语言为繁重的工作而学习 not 代码。你。
如果您不熟悉正则表达式,可以访问此site
答案 1 :(得分:0)
使用 java.text.BreakIterator 包中的BreakIterator类
使用BreakIterator,您可以在句子中找到许多单词......
import java.text.BreakIterator;
BreakIterator boundary = BreakIterator.getWordInstance();
BreakIterator能够做更多...... 喜欢找到字符,行,句子等...
有关详细信息,请参阅此链接:
http://docs.oracle.com/javase/6/docs/api/java/text/BreakIterator.html
答案 2 :(得分:0)
如果他们在单词之间有几个空格怎么办?
使用此选项仅用一个空格替换两个或多个空格,然后继续算法
String stringOnlyOneSpace = stringTwoOrMoreSpaces.trim().replaceAll(" +", " ");