矩阵没有插入所有组合?

时间:2017-08-17 15:03:45

标签: java matrix combinations

这是我的主题矩阵....现在我想在主题之间制定条件

        mat     pro     
mat     null    null    
pro     null    null    

例如关于主题数学我在主题semester =1上插入programing的主题值我放semester = 4所以我想用" yes&填充此矩阵#34;,仅当第一个主题的semester大于第二个主题的学期且" no"如果不是......现在我遇到的问题是我可以为组合输入条件:

math - math , which would be "no"
math - programing , which would be "no"
programing - programing , which would be "no"

,程序完成时看起来像这样:

        mat     pro     
mat     no      no  
pro     no      no

但我没有要求这个组合提出条件: 编程 - 数学,这将是"是" 它应该看起来像这样:

        mat     pro     
mat     no      no  
pro     yes     no

我该如何实现?任何想法?

这是我的代码:

public void makeGraph() {

    matrix = new String[subjects.size()][subjects.size()];

    for (int i = 0; i < matrix.length; i++) {
        for (int j = i; j < matrix.length; j++) {

            System.out.println("Enter conditionality between subjects: " + subjects.get(i).getName() + ", and "
                    + subjects.get(j).getName());

            boolean conditioned = s.nextBoolean();

            if (conditioned == true) {
                if (subjects.get(i).getSemester() > subjects.get(j).getSemester()) {
                    matrix[i][j] = "yes";
                    matrix[j][i] = "yes";
                } else {    
                        matrix[i][j] = "no";
                        matrix[j][i] = "no";    
                }
            } else {
                matrix[i][j] = "no";
                matrix[j][i] = "no";
            }
        }
    }
}
编辑:这是pastebin的链接,我解决了它。 Solution

2 个答案:

答案 0 :(得分:1)

问题在于您分配矩阵的方式。当编程与数学在比例上并且您选择比较时,您为[i] [j]分配与[j] [i]相同的值。那是不对的。一个应该设置为no,另一个应该设置为yes,因为一个人询问Math是否有比编程更多的学期,另一个询问编程是否有比Math更多的学期。既然你正在寻找更大而不是更大的平等,那么其中一个问题就是&#34;是&#34;意味着另一个是&#34; no&#34;。

答案 1 :(得分:1)

我已经解决了你可以查看代码:

public void makeGraph() {

    matrix = new String[subjects.size()][subjects.size()];

    for (int i = 0; i < matrix.length; i++) {
        for (int j = i; j < matrix.length; j++) {

            boolean conditioned;

            if(subjects.get(i).getName().equalsIgnoreCase(subjects.get(j).getName())){
                conditioned=false;
            }else{
                System.out.println("Enter conditionality between subjects: " + subjects.get(i).getName() + ", and "
                        + subjects.get(j).getName());
                conditioned = s.nextBoolean();
            }

            if (conditioned == true) {

                if (subjects.get(i).getSemester() > subjects.get(j).getSemester()) {
                    matrix[i][j] = "yes";
                    matrix[j][i] = "no";
                }else if(subjects.get(i).getSemester()<subjects.get(j).getSemester()){

                    System.out.println("Enter conditionality between subjects: " + subjects.get(j).getName() + ", and "
                            + subjects.get(i).getName());
                    conditioned = s.nextBoolean();
                    matrix[i][j] = "no";
                    matrix[j][i] = "yes";   
                }else{  
                    matrix[i][j] = "no";
                    matrix[j][i] = "no";    
                }
            } else {
                matrix[i][j] = "no";
            }
        }
    }
}