Java 如何计算单词在数组中出现的次数
我试图在java中计算每个单词在数组中的次数,然后显示它,但我不知道如何使用扫描仪添加到数组中,然后试图找到一个方法,该方法将遍历数组并显示每个单词在该数组中的次数Java 如何计算单词在数组中出现的次数,java,arrays,count,Java,Arrays,Count,我试图在java中计算每个单词在数组中的次数,然后显示它,但我不知道如何使用扫描仪添加到数组中,然后试图找到一个方法,该方法将遍历数组并显示每个单词在该数组中的次数 public class Counting { static String[] words = new String[3]; //static int[] aCounts; private static int count; public static void countTimesWordApp
public class Counting {
static String[] words = new String[3];
//static int[] aCounts;
private static int count;
public static void countTimesWordApperesInArray() {
int size = words.length;
for (int i = 0; i < size; i++) {
int position = i;
int count = 0;
for (int j = 0; j < size; j++) {
String element = words[i];
if (words[i].contains(element)) {
count++;
}
}
System.out.println(words[i] + " " + count);
}
}
public static void main(String[] args) {
System.out.println("Enter three Words");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
while (!("-1").equals(input)) {
words[count] = input;
count++;
input = scanner.next();
}
//print();
countDigits();
}
}
公共类计数{
静态字符串[]字=新字符串[3];
//静态int[]a计数;
私有静态整数计数;
公共静态void countTimesWordApperesInArray(){
int size=words.length;
对于(int i=0;i
更改单词[i]。包含(元素)
到单词[j]。等于(元素)
publicstaticvoidcounttimeswordapperesinarray(){
int size=words.length;
对于(int i=0;i
您遇到的问题是什么?错误消息?错误的行为?对countDigits
的调用是否应该调用countTimesWordApperesInArray()
?
public static void countTimesWordApperesInArray() {
int size = words.length;
for (int i = 0; i < size; i += 1) {
int count = 0;
String element = words[i];
for (int j = 0; j < size; j += 1) {
if (words[j].equals(element)) {
count += 1;
}
}
System.out.println(words[i] + " " + count);
}
}