我正在尝试输出字符串中的所有字母及其频率,但是仅出现a
。我以为我的逻辑是正确的,但似乎我遗漏了一些东西。
我在做什么错,我该如何解决?
这是我的代码:
public static void solution(String s) {
char[] c = s.toCharArray();
int j = 0, i = 0, counter = 0;
for(i = 1; i < c.length; i++) {
if(c[i] != c[j]) {
i++;
} else {
counter++;
}
}
System.out.println("The letter " + c[j] + " appears " + counter + " times");
}
public static void main(String args[]) {
String s = "abaababcdelkm";
solution(s);
}
输出:
The letter a appears 1 times
答案 0 :(得分:3)
每次循环时,您将i递增两次。因此,您仅测试其他所有字符。在循环内部,使用continue代替i ++。
public static void solution(String s) {
char[] c = s.toCharArray();
int j = 0, i = 0, counter = 0;
for(i = 1; i < c.length; i++) {
if(c[i] != c[j]) {
continue;
} else {
counter++;
}
}
System.out.println("The letter " + c[j] + " appears " + counter + " times");
}
请注意,此代码将告诉您字符串中的第一个字符出现在字符串其余部分中的次数。也许这就是您想要的,但您的问题尚不清楚。
答案 1 :(得分:1)
您的代码中没有任何预防措施可以使代码在功能步骤中不算相同的内容。在这里,我只是修改了您的代码以使其正常工作。但是您可以将其与我提供的其他版本进行比较,以防止重复计算。
public class Main {
public static void solution(String s) {
char[] c = s.toCharArray();
int j = 0, i = 0, counter = 0;
for (i = 0; i < c.length; i++) {
for (j = i; j < c.length; j++) {
if (c[i] == c[j]) {
counter++;
}
}
System.out.println("The letter " + c[i] + " appears " + counter + " times");
counter = 0;
}
}
public static void main(String args[]) {
String s = "abaababcdelkm";
solution(s);
}
}
输出:
The letter a appears 4 times
The letter b appears 3 times
The letter a appears 3 times
The letter a appears 2 times
The letter b appears 2 times
The letter a appears 1 times
The letter b appears 1 times
The letter c appears 1 times
The letter d appears 1 times
The letter e appears 1 times
The letter l appears 1 times
The letter k appears 1 times
The letter m appears 1 times
答案 2 :(得分:0)
我添加了Arraylist来跟踪已计数的字符,每个字符将只打印一次
import java.util.ArrayList;
public class Main {
public static void solution(String s) {
char[] c = s.toCharArray();
ArrayList<Character> arrayList;
arrayList = new ArrayList<Character>();
int j = 0, i = 0, counter = 0;
for (i = 0; i < c.length; i++) {
if (!arrayList.contains(c[i])) {
arrayList.add(c[i]);
for (j = i; j < c.length; j++) {
if (c[i] == c[j]) {
counter++;
}
}
System.out.println("The letter " + c[i] + " appears " + counter + " times");
counter = 0;
} else {
continue;
}
}
}
public static void main(String args[]) {
String s = "abaababcdelkm";
solution(s);
}
}
输出:
The letter a appears 4 times
The letter b appears 3 times
The letter c appears 1 times
The letter d appears 1 times
The letter e appears 1 times
The letter l appears 1 times
The letter k appears 1 times
The letter m appears 1 times
答案 3 :(得分:0)
这是Maps的有效方式
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
String s = "abaababcdelkm";
Map<Character,Integer> frequency = new TreeMap<Character,Integer>();
for (int i = 0 ; i< s.length() ; i++) {
char ch = s.charAt(i);
if (frequency.containsKey(ch)) {
frequency.replace(ch, frequency.get(ch) + 1);
}
else {
frequency.put(ch, 1);
}
}
for (Map.Entry<Character,Integer> entry : frequency.entrySet()) {
System.out.println("The letter " + entry.getKey() + " appears " + entry.getValue() + " times");
}
}
}