【问题标题】:Matrix not inserting all combinations?矩阵没有插入所有组合?
【发布时间】:2017-08-17 15:03:45
【问题描述】:

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

        mat     pro     
mat     null    null    
pro     null    null    

例如,在主题数学上,我在主题 programing 上插入了 semester =1 的主题值,我输入了 semester = 4,所以我想用“yes”填充这个矩阵,前提是第一个主题的 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

【问题讨论】:

  • 你的帖子写得太差了,请努力..
  • @azro 很抱歉,我知道但我无法理解,尝试正确编写时它对我不起作用。
  • 如果我的解决方案有帮助,您能否通过单击我的答案旁边的灰色复选标记将其变为绿色来接受它?此外,非常感谢点击我的答案旁边的箭头:)
  • @Assafs 我什至没看你回答..当我完成解决方案时才看到它......现在我明白你在答案中的意思与我想的一样,它是一个很好的练习,我想哈哈哈,是的,我会点击复选标记和箭头:)。

标签: java matrix combinations


【解决方案1】:

问题在于您分配矩阵的方式。当编程与数学在规模上并且您选择比较时,您为 [i][j] 分配了与 [j][i] 相同的值。这是不正确的。一个应该设置为 no,另一个设置为 yes,因为一个询问数学是否比编程有更多的学期,另一个询问编程是否比数学有更多的学期。由于您正在寻找更大而不是更大的平等,那么这些问题之一是“是”意味着另一个是“否”。

【讨论】:

    【解决方案2】:

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

    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";
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多