提问人:syed abdullah 提问时间:10/30/2023 更新时间:10/30/2023 访问量:24
在此方案中如何获取输入?我的解决方案得到 0 输入
how do i get an input in this scenario? im getting 0 input with my solution
问:
定义一个名为 getWordFrequency 的方法,该方法将字符串数组、数组大小和搜索词作为参数。然后,方法 getWordFrequency() 返回数组参数中搜索词的出现次数(不区分大小写)。
然后,编写一个主程序,将单词列表读入数组,重复调用方法 getWordFrequency(),并输出数组中的单词及其频率。输入以一个整数开头,表示后面的单词数。假设列表始终包含少于 20 个单词。
例如:如果输入为:
5 嘿嗨马克嗨马克 输出为:
嘿 1 嗨 2 马可福音 2 嗨 2 马可福音 2 提示:使用 equalsIgnoreCase() 方法比较字符串,忽略大小写。
程序必须定义并调用一个方法: public static int getWordFrequency(String[] wordsList, int listSize, String currWord)
这是我的解决方案
导入 java.util.Scanner;
公共类 LabProgram {
public static int getWordFrequency(String[] wordsList, int listSize, String currWord) { int 频率 = 0;
for (int i = 0; i < listSize; i++) {
if (wordsList[i].equalsIgnoreCase(currWord)) {
frequency++;
}
}
return frequency;
}
public static void main(String[] 参数) { 扫描仪 scnr = new Scanner(System.in);
// Read the number of words in the list
int numWords = scnr.nextInt();
scnr.nextLine(); // Consume the newline character
// Create an array to store the words
String[] wordsList = new String[numWords];
// Read the list of words into the array
for (int i = 0; i < numWords; i++) {
wordsList[i] = scnr.next();
}
// Call the getWordFrequency method and output word frequencies
for (int i = 0; i < numWords; i++) {
String currWord = wordsList[i];
int frequency = getWordFrequency(wordsList, numWords, currWord);
if (i == 0 || !currWord.equalsIgnoreCase(wordsList[i - 1])) {
System.out.println(currWord + " " + frequency);
}
}
} }
答: 暂无答案
评论