当我设置它时,为什么这个变量不会改变

时间:2014-05-20 01:16:21

标签: java methods

我有一个名为setLevel

的方法
/**
 * Sets the level of this course.
 * 
 * @param newLevel - one of the enumerated levels
 */
public void setLevel(char newLevel)
{
    if(newLevel == 7 || newLevel == 1 || newLevel == 9 || newLevel == 8)
    {
        level = newLevel;
    }
    else
    {
        level = DEFAULT_LEVEL;
    }
} // end of method setLevel(char newLevel)

当我使用这种方法时,我设置了Level 我有另一种叫做getLevel的方法

/**
 * Returns the level of this course.
 * 
 * @return the level of this course
 */
public char getLevel()
{
    return level;
} // end of method getLevel()

但是当我调用此方法时,它会显示我设置的默认级别。

关于如何修复它的任何想法

这是完整的代码

public class Course extends Object implements Serializable
{
// instance variables 
private int academicYear;  
private String code; 
private static int counter = 0;
private char level; 
private String name;
private int serialNumber;

// class constants 
private static final String DATA_FILE = "courses.data";
public static final String DEFAULT_CODE = "Introduction to Computer Science";
public static final char DEFAULT_LEVEL = 7; 
public static final String DEFAULT_NAME = "ICS3U"; 
public static final int DEFAULT_YEAR = 2014;    
private static final int ERROR_CLASS_NOT_FOUND_EXCEPTION = -1;
private static final int ERROR_EOF_EXCEPTION = -2;
private static final int ERROR_IO_EXCEPTION = -3;
final char[] LEVEL = {'7', '1', '9', '8'};

/**
 * Constructor for objects of class Course
 */
public Course()
{

    name = DEFAULT_NAME; 
    code = DEFAULT_CODE; 
    level = DEFAULT_LEVEL; 
    academicYear = DEFAULT_YEAR;
    serialNumber = ++counter;

} // end of constructor Course()

public Course(String name, String code, char level, int academicYear)
{
    this.name = name; 
    this.code = code;
    if (level == 7 || level == 1 || level == 9 || level == 8)
    {
        level = level; 
    }
    else
    {
        level = DEFAULT_LEVEL;
    }
    if (academicYear > 1900 && academicYear < 2014)
    {
        this.academicYear = academicYear;
    }
    else 
    {
        academicYear = DEFAULT_YEAR;
    }

} // end of constructor Course(String name, String code, char level, int academicYear) 

/**
 * Indicates whether another object has a state identical to this object’s state.
 * 
 * @param otherCourse - the object whose state is compared to this object’s
 */
public boolean equals(Object otherCourse) 
{ 
    if (otherCourse == null) return false;
    if (this.getClass() != otherCourse.getClass()) return false;
    if (this == otherCourse) return true;
    if(this != otherCourse) return false;

    // needed to satisfy the compiler
    return true;

} // end of method boolean equals(Object course) 

/**
 * Compares this Course to another.
 */
public int compareTo(Course otherCourse)
{

    int before = -1;
    int after = 1;
    int equals = 0;
    int resultCode = code.compareTo(otherCourse.getCode());

    if(otherCourse == null) return before ; 
    if(serialNumber < otherCourse.getSerialNumber()) return before;
    if(serialNumber > otherCourse.getSerialNumber()) return after;
    if(code.compareTo(otherCourse.getCode()) != 0) return resultCode;

    if(!(sortLevel(level) == -1 || sortLevel(otherCourse.getLevel()) == -1))
    {
        if(sortLevel(level) < sortLevel(otherCourse.getLevel())) return -1;
        if(sortLevel(level) > sortLevel(otherCourse.getLevel())) return 1;
    }

    return 1;

}

private static int sortLevel(char level) 
{ 
    final char[] LEVEL = {'7', '1', '9', '8'};
    for (int index = 0; index < LEVEL.length; index++) 
    {
        if(LEVEL[index] == level) return index;
        if(LEVEL[index] == level) return DEFAULT_LEVEL;
    }

    // error code for not found, should not be reached
    return -1;
}

/* accessors*/

/**
 * Returns the code of this course.
 * 
 * @returns Returns the code of this course
 */
public String getCode()
{
    return code;
} // end of method getCode()

/**
 * Returns the level of this course.
 * 
 * @return the level of this course
 */
public char getLevel()
{
    return level;
} // end of method getLevel()

/**
 * Returns the name of this course.
 * 
 * @return the name of this course.
 */
public String getName()
{
    return name;
} // end of method getName()

/**
 * Returns the unique serial number of this course.
 * 
 * @return the unique serial number of this course.
 */
public int getSerialNumber()
{
    return serialNumber;
} // end of method getSerialNumber()

/**
 * Returns the academic year of this course.
 * 
 * @return the 4-digit academic year of this course
 */
public int getYear()
{
    return academicYear;
} // end of method getYear()

/* mutators */
/**
 * Sets the code of this course.
 * 
 * @param newCode - the new code of this course.
 */
public void setCode(String newCode)
{
    this.code = newCode;
} // end of method setCode(String newCode)

/**
 * Sets the level of this course.
 * 
 * @param newLevel - one of the enumerated levels
 */
public void setLevel(char newLevel)
{
    if(newLevel == 7 || newLevel == 1 || newLevel == 9 || newLevel == 8)
    {
        level = newLevel;
    }
    else
    {
        level = DEFAULT_LEVEL;
    }
} // end of method setLevel(char newLevel)

/**
 * Sets the name of this course.
 * 
 * @param newName - the new name of this course
 */
public void setName(String newName)
{
    this.name = newName;
} // end of method setName(String newName)

/**
 * Sets the academic year of this course.
 * 
 * @param newYear - the new 4-digit academic year of this course
 */
public void setYear(int newYear)
{
    if (newYear >= 1900 && newYear <= 2014)
    {
        this.academicYear = newYear;
    }
    else
    {
        academicYear = DEFAULT_YEAR;
    }
} // end of method setYear(int newYear)

/**
 * Returns a string representation of this course.
 * 
 * @override toString in class Object
 * 
 * @return a string representation of this course.
 */
public String toString()
{      
    return 
    this.getClass().getName() 
    +"[" 
    + "Serial Number: " + serialNumber
    + ", name: " + name
    + ", code: " + code
    + ", level: " + level
    + ", academic year: " + academicYear
    +"]";
} // end of String toString()
} // end of class Course

3 个答案:

答案 0 :(得分:2)

您已使用字符值&#39; 7&#39;,&#39; 1&#39;,&#39; 9&#39;,&#39; 8&#39;初始化了LEVEL。但是你的setLevel方法将它们与整数值7,1,9,8进行比较。

他们不是一回事。

坦率地说,如果LEVEL值是整数,那么你应该将它们声明为int。通过将其存储为字符,您无法实现任何目标。

答案 1 :(得分:0)

将newLevel与值进行比较时。

您实际上是将newLevel的ASCII值与整数进行比较。 这就是每次它给出Default_level作为答案的原因。 使用单引号进行比较,例如。 &#39; 7&#39;

答案 2 :(得分:0)

我认为你应该在比较char值

时添加单引号
public void setLevel(char newLevel)
{
    if(newLevel == '7' || newLevel == '1' || newLevel == '9' || newLevel == '8')
    {
        level = newLevel;
    }
    else
    {
        level = DEFAULT_LEVEL;
    }
}