数字母
Counting Letters
我在完成这段代码时遇到了一些困难。基本上,我必须计算一个字母在给定字符串中出现的次数。比如ABA应该输出如下
"A appears 1 times"
"B Appears 1 times"
"A Appears 1 times"
然而,我编写的以下代码执行以下操作(它是方法的一部分)
public static char[] counter(char[] original, char[] manipulated) {
int counter =0;
for (int i=0; i<manipulated.length; i++) {
for (int j=0; j<original.length; j++) {
if (original[j] == manipulated[i]) {
counter++;
} else {
}
}
System.out.println(manipulated[i] + " appears " + counter + " times");
counter = 0;
}
return manipulated;
}
输出是这样的:
"A appears 2 times"
"B appears 1 times"
"A appears 2 times"
这并没有错,但这不是我想要的。那么,您能否尽快协助我解决这个问题。我知道我应该重置一些变量,但我不确定实际重置它的位置。
*一些注意事项:
操作的变量只是不包含重复项的字符串
原始的是 abaa,被操纵的是 aba :)
如果您想知道同一个字符在一行中出现了多少次,您可以将代码更改为
public static void main(String[] args) {
System.out.println(counter("AAACCAAAAAAAB"));
}
public static String counter(String s) {
int counter = 0;
int j;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); ) {
j = i;
char c = s.charAt(i);
for (; j < s.length(); j++) {
if (counter == 0) {
sb.append(c);
}
if (c == s.charAt(j)) {
counter++;
} else {
break;
}
}
System.out.println(s.charAt(i) + " appears " + counter + " times");
counter = 0;
i = j;
}
return sb.toString();
}
UPDATE 更改了代码,这样您就不需要提供两个 char[] - 现在您可以输入任何字符串,例如 "AAACCAAAAAAAB"(又名 'original' ) 和以前称为 'manipulated' 的 char[] 将由该方法返回。
结果:
A appears 3 times
C appears 2 times
A appears 7 times
B appears 1 times
ACAB
如果您的预期行为只是 "print out the number of times each character appears in a row" 那么我完全不确定您为什么需要 manipulated
变量。您当前的代码只是接受它作为参数,然后 returns 它保持不变。
所以除非我误解了这个问题,否则它可能是一个更简单的问题:
void showConsecutiveCharacterCounts(char[] input) {
int consecutiveCount = 0;
/* iterate through all chars */
for (int i = 0; i < input.length; i++) {
/* increment count - will be 1 first time */
consecutiveCount++;
/* if we are at the end of a sequence of chars
I.e. end of input OR next char does not match */
if (i == input.length - 1 || input[i] != input[i + 1]) {
/* print previous sequence */
System.out.println(input[i] + " appears " + consecutiveCount + " times");
/* and reset count */
consecutiveCount = 0;
}
}
}
我在完成这段代码时遇到了一些困难。基本上,我必须计算一个字母在给定字符串中出现的次数。比如ABA应该输出如下
"A appears 1 times"
"B Appears 1 times"
"A Appears 1 times"
然而,我编写的以下代码执行以下操作(它是方法的一部分)
public static char[] counter(char[] original, char[] manipulated) {
int counter =0;
for (int i=0; i<manipulated.length; i++) {
for (int j=0; j<original.length; j++) {
if (original[j] == manipulated[i]) {
counter++;
} else {
}
}
System.out.println(manipulated[i] + " appears " + counter + " times");
counter = 0;
}
return manipulated;
}
输出是这样的:
"A appears 2 times"
"B appears 1 times"
"A appears 2 times"
这并没有错,但这不是我想要的。那么,您能否尽快协助我解决这个问题。我知道我应该重置一些变量,但我不确定实际重置它的位置。
*一些注意事项: 操作的变量只是不包含重复项的字符串 原始的是 abaa,被操纵的是 aba :)
如果您想知道同一个字符在一行中出现了多少次,您可以将代码更改为
public static void main(String[] args) {
System.out.println(counter("AAACCAAAAAAAB"));
}
public static String counter(String s) {
int counter = 0;
int j;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); ) {
j = i;
char c = s.charAt(i);
for (; j < s.length(); j++) {
if (counter == 0) {
sb.append(c);
}
if (c == s.charAt(j)) {
counter++;
} else {
break;
}
}
System.out.println(s.charAt(i) + " appears " + counter + " times");
counter = 0;
i = j;
}
return sb.toString();
}
UPDATE 更改了代码,这样您就不需要提供两个 char[] - 现在您可以输入任何字符串,例如 "AAACCAAAAAAAB"(又名 'original' ) 和以前称为 'manipulated' 的 char[] 将由该方法返回。
结果:
A appears 3 times
C appears 2 times
A appears 7 times
B appears 1 times
ACAB
如果您的预期行为只是 "print out the number of times each character appears in a row" 那么我完全不确定您为什么需要 manipulated
变量。您当前的代码只是接受它作为参数,然后 returns 它保持不变。
所以除非我误解了这个问题,否则它可能是一个更简单的问题:
void showConsecutiveCharacterCounts(char[] input) {
int consecutiveCount = 0;
/* iterate through all chars */
for (int i = 0; i < input.length; i++) {
/* increment count - will be 1 first time */
consecutiveCount++;
/* if we are at the end of a sequence of chars
I.e. end of input OR next char does not match */
if (i == input.length - 1 || input[i] != input[i + 1]) {
/* print previous sequence */
System.out.println(input[i] + " appears " + consecutiveCount + " times");
/* and reset count */
consecutiveCount = 0;
}
}
}