2017-08-17 70 views
1

这里是我的主题矩阵....现在我想使被摄物体矩阵没有插入所有组合?

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

编辑:这里是引擎收录我已经解决了它的链接。 Solution

+0

你的职位是这么写的不好,请作出努力.. – azro

+0

@azro我很抱歉,我知道,但我不能让它,它试图正确写入它不起作用。 –

+0

如果我的解决方案有帮助,请点击我答案旁边的灰色复选标记,接受它,让它变绿吗?此外,单击我的答案旁边的箭头将不胜感激:) – Assafs

回答

1

问题出在您分配矩阵的方式。当编程与数学在规模上并且您选择比较时,您可以像[j] [i]一样为[i] [j]赋值相同的值。这是不正确的。一个应该设置为no,另一个设置为yes,因为有人问Math有更多的学期而不是Programming,另一个问是否Programming有更多的学期而不是Math。既然你在寻找更大而不是更大的平等,那么这些问题中的一个是“是”意味着另一个是“否”。

+0

查看“编辑”我解决了它 –

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