2012-03-06 115 views
0

我正在尋找一種方法來計算在Java中輸入值的次數。更具體地說,我必須管理一個Module類,它的屬性爲:semestercode。如果它是第一學期添加的第一個模塊,其代碼應該是101.該學期的第二個將是102,同樣,第二學期輸入的第一個模塊將是201等等。Java:計算輸入值的次數

我試過使用一個靜態實例變量count,但我無法設法使其根據輸入的學期的值進行更改。

任何建議表示讚賞。提前致謝!

- EDIT--

下面是我班的完整代碼。如果太長,我很抱歉。

package courseman; 

/** 
* @overview <code>Module</code> represents the module object of a CourseMan program 
* @author 
* 
*/ 
public class Module { 
    private static int count = 1; 
    private String name; 
    private int semester; 
    private int credits; 
    private String code; 

/** 
* @effects Initializes a <code>Module</code> object 
* 
*/ 
public Module(){ 
    name = "No name"; 
    semester = 1; 
    credits = 0; 
    code = "M000"; 
} 

/** 
* @param newModule a <code>Module</code> object 
* @effects   Creates a <code>Module</code> object with <code>newModule</code> 
* 
*/ 
public Module (Module newModule){ 
    if (newModule == null){ 
     System.out.println("Fatal error creating module."); 
     System.exit(0); 
    } 
    name = newModule.name; 
    semester = newModule.semester; 
    credits = newModule.credits; 
    code = newModule.code; 
} 

/** 
* 
* @param newName  a <code>String</code> to be set as a module's name 
* @param newSemester an <code>integer</code> to be set as a module's semester 
* @param newCredits an <code>integer</code> to be set as a module's credits 
* @effects    Creates a new <code>Module</code> object with predefined name, semester and credits 
*/ 
public Module(String newName, int newSemester, int newCredits){ 
    if(newName == null || newSemester <= 0 || newCredits <= 0){ 
     System.out.println("Fatal error creating module."); 
     System.exit(0); 
    } 
    name = newName; 
    semester = newSemester; 
    credits = newCredits; 

    int no = semester*100 + count; 
    code = "M" + no; 
    count++; 
} 

/** 
* 
* @param newName a <code>String</code> to be set as a module's name 
* @modifies  <code>this.name</code> 
* @effects   Takes <code>newName</code> to update a module's name 
* 
*/ 
public void setName(String newName){ 
    if(newName == null){ 
     System.out.println("Fatal error setting module's name."); 
     System.exit(0); 
    } 
    name = newName; 
} 

/** 
* 
* @param newSemester an <code>int</code> to be set as a module's semester 
* @modifies   <code>this.semester</code> 
* @effects    Takes <code>newSemester</code> to update a module's semester 
* 
*/ 
public void setSemester(int newSemester){ 
    if (newSemester <= 0){ 
     System.out.println("Fatal error setting module's semester."); 
     System.exit(0); 
    } 
    semester = newSemester; 
    int no = (int) Integer.parseInt(code.substring(1)); 
    if (((no - count - 1)/100) < semester){ 
     no = semester*100 + (count - 1); 
     code = "M" + no; 
    } 
} 

/** 
* 
* @param newCredits an <code>integer</code> to be set as a module's number of credits 
* @modifies   <code>this.credits</code> 
* @effects    Takes <code>newCredits</code> to update a module's number of credits 
* 
*/ 
public void setCredits (int newCredits){ 
    if (newCredits <= 0){ 
     System.out.println("Fatal error setting module's credits."); 
     System.exit(0); 
    } 
    credits = newCredits; 
} 

/** 
* 
* @effects Returns a module's code 
*/ 
public String getCode(){ 
    return code; 
} 

/** 
* @effects Returns a module's name 
* 
*/ 
public String getName(){ 
    return name; 
} 

/** 
* @effects Returns a module's semester 
* 
*/ 
public int getSemester(){ 
    return semester; 
} 

/** 
* @effects Returns a module's number of credits 
* 
*/ 
public int getCredits(){ 
    return credits; 
} 

/** 
* @effects Returns <code>String</code> representation of a <code>Module</code> object 
*/ 
public String toString(){ 
    return "Module: " + code + " - " + name + " - semester " + semester + " - " + credits + " credits"; 
} 

/** 
* 
* @param otherModule a <code>Module</code> object to compare 
* @effects    Returns <code>true</code> if <code>this</code> points to the same 
*      object as <code>otherModule</code>, otherwise returns <code>false</code> 
*/ 
public boolean equals(Module otherModule){ 
    return (code.equals(otherModule.code)); 
} 

}

+0

請張貼一些代碼,以便我們能夠更好地理解這個問題。 – 2012-03-06 03:25:49

+0

請發佈您當前的代碼,並告訴我們它是如何失敗的。 – Synesso 2012-03-06 03:25:59

回答

1

你應該爲每學期設置一個單獨的櫃檯!嘗試使用將學期映射到該學期課程數目的靜態HashMap。當添加新的Module時,您需要增加計數(如果沒有,則將其初始化爲1)。

這裏是一個骨架:

class Module { 
    private static HashMap<Integer, Integer> courseCounts = new HashMap<Integer, Integer>(); 

    public Module(int semester, String name) { 
     this.semester = semester; 
     this.name = name; 
     Integer count = courseCounts.get(semester); 
     if (count == null) 
      count = 0; 
     ++count; 
     this.code = "M" + (semester * 100 + count); 
     courseCounts.put(semester, count); 
    } 
} 
+0

HashMap看起來是個不錯的主意。這對我來說是新的,但我會嘗試使用它。感謝您的建議! – 2012-03-06 03:34:26

+0

很高興幫助。需要了解的是,只要你開始爲不同的學期添加模塊,你需要「不同的靜態計數」,因此一個靜態計數不會執行。任何一種'Map'都可以,但'HashMap'在這種情況下是最好的。 – Irfy 2012-03-06 03:36:21

0

編輯:

我以爲semester是它自己的類。如果您想要爲每個學期分別計數,您需要創建一個計數列表,您可以使用Map做到這一點,Semester作爲關鍵字,它的count作爲其值。

+0

是的,這是伎倆。謝謝! – 2012-03-06 03:55:54