【发布时间】:2014-12-08 07:38:10
【问题描述】:
如果属实,我需要进行一些关键字搜索和打印。 如果我按顺序比较,效果很好。但我想
比较以下情况并期望它们是正确的。
做一些java编程=真
一些java = true
做编程 = true
最后是最重要的
编程 java = true
编程 java some do = true
对于上面提到的所有情况,我都需要返回 true,但到目前为止它只适用于情况 1 和 2
public class Examples {
public static void main(String[] args) {
String[] given = new String[20];
given[0] = ("do some java programming");
given[1] = ("do some grocery shopping");
given[2] = ("play soccer at the west field");
String input = new String();
Scanner userInput = new Scanner(System.in);
System.out.println("Enter the string to compare");
input[0] = userInput.nextLine();
for (int i=0; i <20; i++){
if(given[i].contains(input))
{
System.out.println(given[i]);
}
else
{
//do nothing
}
}
}
}
【问题讨论】:
-
您是否愿意接受比您的更清洁的其他解决方案?像 swtich 语句
-
是的,我也需要编辑我的输入。它是字符串而不是字符串数组。
标签: java search compare contains matcher