【发布时间】:2017-03-11 05:36:25
【问题描述】:
我被一个问题困扰了很长时间,这需要我计算一个术语 x 术语矩阵。所以,我有 2 个数组,分别是 keywords 和 sentences,如下所示:
String[] Keywords = {"Human", "Machine", "Interface", "Application" };
String[] Sentence = {"Human is Machine", "Interface of Robot", "Application on Human"};
接下来,我必须将它们制成表格,如下图所示。
逻辑:
- 如果行和列是相同的关键字,我们就输入 0。
- 在 Human(row) x Machine(column) 空间中,我们放 1,因为这两个
单词在同一个句子中出现一次(即第一句
在数组中)。
- 在 Human(row) x Interface(column) 中,我们输入 0,因为这两个词
两个句子都不一起存在。
- 搜索不区分大小写。
- 然后进入下一列,然后是下一行。
这是我尝试过的,但不知何故出了点问题。
public class Test {
public static int [][] matrix;
public static void main(String[] args) throws Exception {
String[] Keywords = {"Human", "Machine", "Interface", "Application" };
String[] Sentence = {"Human is Machine", "Interface of Robot", "Application on Human"};
int [][] matrix = new int[Keywords.length][Keywords.length]; //initialize matrix
System.out.println(Arrays.toString(Keywords));
System.out.println("\n"+ Arrays.toString(Sentence));
for (int i=0;i<Keywords.length;i++)
{
int count = 0;
for (int q=1;q<Sentence.length;q++)
{
if (Keywords[i].contains(Sentence[q]))
{
matrix[i][q] = count++;
}
}
}
System.out.println(Arrays.deepToString(matrix));
}
}
感谢任何帮助。谢谢!
【问题讨论】:
标签: java loops matrix multidimensional-array