我正在寻找一种计算Java输入值的次数的方法。更具体地说,我必须管理具有以下属性的Module
类:semester
和code
。如果它是第一学期要添加的模块,其代码应为101.该学期的第二个模块为102,同样,第二学期的第一个模块将为201,依此类推。
我尝试过使用静态实例变量count
,但我无法以某种方式设法根据输入的学期值进行更改。
任何建议表示赞赏。提前谢谢!
- 编辑 -
以下是我班级的全部代码。如果它太冗长,我很抱歉。
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 :(得分:1)
你应该为学期管理一个单独的柜台!尝试使用静态HashMap将学期映射到该学期的课程数。添加新的Module
时,您需要增加 appripriate 计数(如果不存在,则将其初始化为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);
}
}
答案 1 :(得分:0)
编辑:
我认为semester
是它自己的班级。如果您想要为每个学期单独计算一次,您需要创建一个计数列表,您可以使用Map
作为关键字,Semester
为其count
来执行此操作值。