比较 Ascii 码
Comparing the Ascii Code
我需要创建一个 Morsealphabet 并且必须插入 BinaryTree
。我需要创建 isLess
和 isGreater
方法。所以这就像我开始:
public boolean isLess(Buchstabe a) {
if (a != null) {
if (!a.isEqual(this)) {
String aCode = a.getCode();
if (aCode.compareTo(this.getCode()) > 0)
return true;
else
return false;
}
else
return false;
}
else
return false;
}
public boolean isGreater(Buchstabe a) {
if (!a.isEqual(this)) {
String aCode = a.getCode();
if (aCode.compareTo(this.getCode()) > 0)
return false;
else
return true;
}
else
return false;
}
所以语法应该是.必须小于 -
这 。并且 - 可以作为字符串处理,所以你应该能够比较 "."
和 "-.--"
其中 "."
应该小于 "-.--"
- 我的解决方案完全是胡说八道。您有可行的解决方案吗?
".".compareTo("-")
给我 1 所以 "."
在 Java.
中被认为大于 "-"
那么显而易见
if(aCode.compareTo(this.getCode())>0)
return false;
returns 如果 aCode
大于 this.getCode()
则为 false,这与您想要的相反。交换 returns 中的 true
和 false
应该可以解决问题。
您不需要重新发明轮子,因为如果参数是按字典顺序等于此字符串的字符串,compareTo()
方法将返回 0
;如果参数是按字典顺序大于此字符串的字符串,则小于 0
的值;如果参数是按字典顺序小于此字符串的字符串,则该值大于 0
。所以你不需要自己制作方法来做到这一点
假设您的 getCode()
方法获得了正确的代码,我会这样做
String aCode = a.getCode();
int code = aCode.compareTo(this.getCode())
if (code == 0){
// they are equal
}
if (code < 0){
// aCode is greater than this.getCode
}
if (code > 0){
// aCode is less than this.getCode
}
我需要创建一个 Morsealphabet 并且必须插入 BinaryTree
。我需要创建 isLess
和 isGreater
方法。所以这就像我开始:
public boolean isLess(Buchstabe a) {
if (a != null) {
if (!a.isEqual(this)) {
String aCode = a.getCode();
if (aCode.compareTo(this.getCode()) > 0)
return true;
else
return false;
}
else
return false;
}
else
return false;
}
public boolean isGreater(Buchstabe a) {
if (!a.isEqual(this)) {
String aCode = a.getCode();
if (aCode.compareTo(this.getCode()) > 0)
return false;
else
return true;
}
else
return false;
}
所以语法应该是.必须小于 -
这 。并且 - 可以作为字符串处理,所以你应该能够比较 "."
和 "-.--"
其中 "."
应该小于 "-.--"
- 我的解决方案完全是胡说八道。您有可行的解决方案吗?
".".compareTo("-")
给我 1 所以 "."
在 Java.
"-"
那么显而易见
if(aCode.compareTo(this.getCode())>0)
return false;
returns 如果 aCode
大于 this.getCode()
则为 false,这与您想要的相反。交换 returns 中的 true
和 false
应该可以解决问题。
您不需要重新发明轮子,因为如果参数是按字典顺序等于此字符串的字符串,compareTo()
方法将返回 0
;如果参数是按字典顺序大于此字符串的字符串,则小于 0
的值;如果参数是按字典顺序小于此字符串的字符串,则该值大于 0
。所以你不需要自己制作方法来做到这一点
假设您的 getCode()
方法获得了正确的代码,我会这样做
String aCode = a.getCode();
int code = aCode.compareTo(this.getCode())
if (code == 0){
// they are equal
}
if (code < 0){
// aCode is greater than this.getCode
}
if (code > 0){
// aCode is less than this.getCode
}