我正在尋找一種方法來計算在Java中輸入值的次數。更具體地說,我必須管理一個Module
類,它的屬性爲:semester
和code
。如果它是第一學期添加的第一個模塊,其代碼應該是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));
}
}
請張貼一些代碼,以便我們能夠更好地理解這個問題。 – 2012-03-06 03:25:49
請發佈您當前的代碼,並告訴我們它是如何失敗的。 – Synesso 2012-03-06 03:25:59