public class Binary {
private int _DecNum;
private String _BinNum;
public Binary(int n) {
_DecNum = n;
_BinNum = ConvToBinR(n);
}
public static String ConvToBin(int n) {
String a = "";
while(n > 1) {
a = (n % 2) + a;
n = n /2;
}
return n + a;
}
public String toString() {
return _BinNum;
}
public boolean equals(Object a) {
return this == a || (a instanceof Binary && this._BinNum == ((Binary) a)._BinNum);
}
public static void main(String[] args ) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
Binary Zero = new Binary(a);
Binary One = new Binary(b);
System.out.println("Base 10: " + a + " in Base 2: " + Zero);
System.out.println(Zero.equals(One));
}
}
嗨,大家好!由于某种我无法弄清楚的原因,我的 equals 函数不起作用。this._BinNum == ((二进制) a)._BinNum; 即使我将 a 和 b 的值设置为相等,也会给我一个错误的陈述。任何人都可以帮忙吗?谢谢!