我已经可以数元音、辅音、空格和特殊字符了。我必须使用一些初学者技术来做到这一点。[编辑:鉴于最后一条语句和以下代码,这个问题听起来像是对家庭作业的帮助] 没什么太复杂的。
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");
}
}
我怎样才能让程序打印字数?如果它们在单词之间有几个空格怎么办?