【发布时间】:2013-12-02 19:03:41
【问题描述】:
我试图计算一个字符在字符串中连续出现的次数。这个似乎无处可去:\ 如果你能帮助我,我将不胜感激。
提前致谢:)
int totCharacters=0, vowels=0, digits=0, odds=0, consecutive=0, index=0;
String text;
char ch;
Scanner input = new Scanner(System.in);
System.out.print("Please enter a sentence terminated with a full stop or exclamation mark: ");
text = input.nextLine();
do {
ch = text.charAt(index);
if(ch=='.' && ch=='!')
break;
if(index<text.length()-1)
totCharacters++;
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
vowels++;
if(ch>='0' && ch<='9')
digits++;
if(ch=='1' || ch=='3' || ch=='5' || ch=='7' || ch=='9')
odds++;
index++;
}while(ch!= '.' && ch!='!');
for(int i=0; i<text.length(); i++)
{
if(ch==text.charAt(i))
consecutive++;
}
【问题讨论】:
-
改用正则表达式?
-
你的标题是
occurs consecutively,但你的描述只是occurred。是哪一个? -
@CMate 不允许使用正则表达式 :(
-
@peeskillet 对不起,是的,我也应该连续提到