- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我一直在尝试寻找与用户输入的句子相匹配的关键字。我不想输入一个字符串,而是想输入整个句子并检查句子中的关键字。这是我编写的一些代码,但由于某种原因我无法扫描整个句子以查找匹配的关键字。
input = scanner.nextLine().toLowerCase();
for (int i = 0; i < keywords.length; i++) {
if (keywords[i].equalsIgnoreCase(input)) {
System.out.println("Found keyword!");
// TODO: You can optimize this
}
}
关键字列表是
String[] keywords = { "day", "What book", "professor name", "office",
"hour", "e-mail", "name", "major", "student e-mail",
"group id", "group name", "lecture", "lecture room",
"lecture time", "number of lectures", "current lecture",
"topics of current lecture", "number of test",
"date of a test", "number of assignments", "sure",
"current assignment", "due day" };
最佳答案
String[] keywords = {"day", "What book", "professor name", "office",
"hour", "e-mail", "name", "major", "student e-mail",
"group id", "group name", "lecture", "lecture room",
"lecture time", "number of lectures", "current lecture",
"topics of current lecture", "number of test",
"date of a test", "number of assignments", "sure",
"current assignment", "due day"};
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine().toLowerCase();
for (String keyword : keywords) {
if (input.contains(keyword)) {
System.out.println("Found keyword match , " + keyword);
}
尝试上面的代码,它会检查您的输入句子是否包含任何给定的关键字。
关于java - 在 Java 中扫描整个用户输入以查找匹配的关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29573709/
我是一名优秀的程序员,十分优秀!