我正在编写一个程序,要求用户输入关键字和一篇文章,然后检查文章中使用了多少个关键字。
当前输出:
输入关键字:
狗,猫输入论文:
我喜欢猫
线程“main”中的异常java.lang.RuntimeException
期望的输出:
输入关键字:
狗,猫输入论文:
我喜欢猫
1
到目前为止,这是我的代码:
static int keywordsChecker(String shortEssay, String keywords) {
int count = 0;
for (int i = 0; i < keywords.length(); i++) {
String[] ary = keywords.split(",");
if (shortEssay.contains(ary)) {
count++;
}
System.out.println(count);
}
return count;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter keywords: ");
String keyword = input.nextLine();
System.out.println("Enter essay: ");
String essay = input.nextLine();
keywordsChecker(essay, keyword);
}
答案 0 :(得分:2)
这应该是你的方法:
function checkCombo(String mletter) {
String generatedLetter;
switch(letter) {
case "AB":
generatedLetter = 'Z';// or you can set it to your textview here
letter = ""; // if it goes inside any of your switch case clear your
global variable
break;
case "DEF":
generatedLetter = 'P'
letter = "";
break;
}
}
分割关键字。keywords.split(",");
检查数组中的每个值是否在shortEssay
字符串中可用。
shortEssay.contains(ary[index])
变量增加1。count
循环。continue
变量。所以你的count
方法看起来像这样:
keywordsChecker
答案 1 :(得分:2)
static int keywordsChecker(String shortEssay, String keywords)
{
int count = 0;
String[] ary = keywords.split(",");
for (int i = 0; i < ary.length; i++) {
if (shortEssay.contains(ary[i])) {
count++;
}
}
return count;
}
只需要调用此方法并获取关键字计数。 :)
答案 2 :(得分:0)
试试这个:
static int keywordsChecker(String shortEssay, String keywords) {
int count = 0;
for (String keyword : keywords.split(",")) {
if (shortEssay.contains(keyword.trim()) {
count++;
}
}
return count;
}
现在,当您调用该方法时,您需要打印返回值:
int count = keywordsChecker(essay, keyword);
System.out.println(count);
答案 3 :(得分:0)
您的代码应该是这样的:
static int keywordsChecker(String shortEssay, String keywords) {
int count = 0;
String[] ary = keywords.split(",");
for (int i = 0; i < ary.length; i++) {
if (shortEssay.contains(ary[i])) {
count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter keywords: ");
String keyword = input.nextLine();
System.out.println("Enter essay: ");
String essay = input.nextLine();
System.out.println(keywordsChecker(essay, keyword));
}