爲了使事情變得更容易理解和更,我想用Compound
類自己可能工作的面向對象如預期的。
class Compound {
String last;
String compound;
int lastSuffixCount;
Compound() {
this.last = "";
this.compound = "";
this.lastSuffixCount = 0;
}
public String getCompound() {
return compound;
}
public void setCompound(String compound) {
this.compound = compound;
}
public void add(String suffix, int times) {
if (suffix.equals(this.last) && times > 0)
{
this.compound = compound.replace(lastSuffixCount + "", "");
this.lastSuffixCount = lastSuffixCount + times;
this.compound += lastSuffixCount;
} else if (times > 0) {
this.compound += suffix + times;
}
this.lastSuffixCount = times;
this.last = suffix;
}
}
採樣驅動程序:
public static void main(String[] args) {
Compound c = new Compound();
c.add("H", 2);
System.out.println(c.getCompound());
c.add("H", 1);
System.out.println(c.getCompound());
c.add("O", 6);
c.add("H", 4);
System.out.println(c.getCompound());
}
輸出:
H2
H3
H3O6H4
你能提供你期望的功能嗎?我不清楚。 – Pankaj
更具體。一些示例代碼會非常有幫助。 – JimN
我想檢查我添加到它。例如,如果我有H,我想檢查是否添加了另一個'H',或'C'或'N'或'O'。 – Joe24