gpt4 book ai didi

Java:统计一个值被输入的次数

转载 作者:行者123 更新时间:2023-12-01 15:30:30 24 4
gpt4 key购买 nike

我正在寻找一种方法来计算 Java 中输入值的次数。更具体地说,我必须管理一个 Module 类,它具有以下属性:semestercode。如果它是第 1 学期添加的第一个模块,则其代码应为 101。该学期的第二个模块的代码将为 102,同样,第 2 学期输入的第一个模块的代码将为 201,依此类推。

我尝试使用静态实例变量count,但我无法以某种方式设法使其根据输入的学期值进行更改。

如有任何建议,我们将不胜感激。提前致谢!

--编辑--

下面是我的类(class)的完整代码。如果太长,我很抱歉。

    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));
}

}

最佳答案

您应该为学期管理一个单独的柜台!尝试使用静态 HashMap 将学期映射到该学期的类(class)数量。添加新的模块时,您需要增加适当的计数(如果不存在,则将其初始化为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);
}
}

关于Java:统计一个值被输入的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9577495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com