Java,if语句没有返回true boolean

时间:2014-03-28 05:50:41

标签: java

我正在尝试调试此项目,并且在测试期间,在Test类中,虽然已经满足checkModuleSelection()方法返回True的条件,但这不会发生。我希望驱动程序类使用提供的测试数据运行checkModuleSelection()方法并返回true。

测试类

public class Test {
    public static void main (String [] args)
    {

        Module csc8001 = new Module("CSC8001", "Programming and data structures", 15, 0, 0, 6);//1: 60, 2: 60 
        Module csc8002 = new Module("CSC8002", "Test Module", 0, 15, 0, 7);
        Module csc8003 = new Module("CSC8001", "Programming and data structures", 0, 0, 0, 7);
        Module csc8004 = new Module("CSC8002", "Test Module", 15, 0, 0, 7);
        Module csc8005 = new Module("CSC8001", "Programming and data structures", 0, 15, 0, 7);
        Module csc8006 = new Module("CSC8002", "Test Module",  0, 0, 0, 7);
        Module csc8007 = new Module("CSC8001", "Programming and data structures", 15, 0, 0, 7);
        Module csc8008 = new Module("CSC8002", "Test Module",  0, 15, 0, 7);
        Module csc8009 = new Module("CSC8001", "Programming and data structures", 0, 0, 0, 7);
        Module csc8010 = new Module("CSC8002", "Test Module", 15, 0, 0, 7);//lvl 7 150
        Module csc8011 = new Module("CSC8001", "Programming and data structures",  0, 15, 0, 7);
        Module csc8012 = new Module("CSC8002", "Test Module", 0, 0, 60, 7);
        Module csc8013 = new Module("CSC8001", "Programming and data structures", 0, 0, 0, 7);

        Student chris = new Student("Chris", "1"); 
        System.out.println(chris.getName());


        chris.addModule(csc8001);
        chris.addModule(csc8002);
        chris.addModule(csc8003);
        chris.addModule(csc8004);
        chris.addModule(csc8005);
        chris.addModule(csc8006);
        chris.addModule(csc8007);
        chris.addModule(csc8008);
        chris.addModule(csc8009);
        chris.addModule(csc8010);
        chris.addModule(csc8011);
        chris.addModule(csc8012);
        chris.addModule(csc8013);

       System.out.println(chris.checkModuleSelection());



    }

}

学生班

public boolean checkModuleSelection() {

        int sumLevel6Credits = 0;
        int sumLevel7Credits = 0;
        int sumOtherCredits = 0;
        int sem1Credits = 0;
        int sem2Credits = 0;
        int sem3Credits = 0;
boolean projectModule = false;

        for (int i = 0; i < MAX_NUMBER_OF_MODULES; i++) {
            System.out.println("The value of sem1Credits is: " + sem1Credits);
            if (moduleRecords[i] != null) {

                if (moduleRecords[i].getModule().getLevel() == Module.MSC_MODULE_LEVEL) {
                    sumLevel7Credits += moduleRecords[i].getModule()
                            .getSemesterOneCredits();
                    System.out.println();
                } else if (moduleRecords[i].getModule().getLevel() == Module.STAGE_3_MODULE_LEVEL) {
                    sumLevel6Credits += moduleRecords[i].getModule()
                            .getTotalCredits();
                }
                projectModule = moduleRecords[i].getModule().getTotalCredits() >= MINIMUM_PROJECT_CREDITS;
                sem1Credits += moduleRecords[i].getModule()
                        .getSemesterOneCredits();
                sem2Credits += moduleRecords[i].getModule()
                        .getSemesterTwoCredits();
                sem3Credits += moduleRecords[i].getModule()
                        .getSemesterThreeCredits();
            }
        }
        // check that there is at least one project module, there isn't too many
        // level 6 credits, there is enough total credits and enough credits in
        // each semester
        return !((sumOtherCredits > 0)
                || !projectModule
                || (sumLevel6Credits < MAXIMUM_LEVEL_6_CREDITS)
                || (sumLevel6Credits + sumLevel7Credits != VALID_NUMBER_OF_REGISTERED_CREDITS)
                || sem1Credits < 50 || sem1Credits > 70 || sem2Credits < 50
                || sem2Credits > 70 || sem3Credits < 50 || sem3Credits > 70);

    }

修改 根据要求,Module类位于

之下
package Project1;
//This class represents a module
public class Module {

      public final static int MSC_MODULE_PASS_MARK = 50;

      public final static int UG_MODULE_PASS_MARK = 40;
      public final static int MSC_MODULE_LEVEL = 7;
      public final static int STAGE_3_MODULE_LEVEL = 6;
      private String moduleCode;
      private String moduleTitle;
      private int sem1Credits;
      private int sem2Credits;
      private  int sem3Credits;
      private  int moduleLevel;


      public Module(String code, String title, int sem1, int sem2, int sem3, int level)
      {

          moduleCode = code;
          moduleTitle = title;
          sem1Credits = sem1;
          sem2Credits = sem2;
          sem3Credits = sem3;
          moduleLevel = level;

      }

      //method to return the module code
      public String getCode()
      {

          return moduleCode;

      }

    //method to return the module title
      public String getTitle()
      {

          return moduleTitle;

      }
    //method to return the Semester 1 credits
      public int getSemesterOneCredits()
      {

          return sem1Credits;

      }
    //method to return the Semester 2 credits
      public int getSemesterTwoCredits()
      {

          return sem2Credits;

      }
    //method to return the Semester 3 credits
      public int getSemesterThreeCredits()
      {

          return sem3Credits;

      }

    //method to return the total credits across all the semesters
      public int getTotalCredits()
      {

         // return sem2Credits + sem3Credits;
          return sem1Credits + sem2Credits + sem3Credits;

      }

    //method to return the module level
      public int getLevel()
      {

          return moduleLevel;

      }

}

1 个答案:

答案 0 :(得分:2)

使用.equals()方法进行比较,不要使用==来比较对象。

==将检查两个引用是否在内存中引用相同的对象,而用于比较两个对象的.equals()意味着相等。

if (moduleRecords[i].getModule().getLevel().equals(Module.MSC_MODULE_LEVEL)) {}
相关问题