gpt4 book ai didi

java - 字符串比较没有返回正确的答案

转载 作者:行者123 更新时间:2023-12-02 06:47:19 25 4
gpt4 key购买 nike

我想使用compareTo()函数来比较两个字符串。

示例:

int result = "650".compareTo("651");
if(result < 0){
System.out.println("smaller");
} else if(result > 0){
System.out.println("bigger");
} else {
System.out.println("equals");
}
System.out.println(result);

这将输出更小,这是正确的。

示例2:

int result = "650".compareTo("1000");
if(result < 0){
System.out.println("smaller");
} else if(result > 0){
System.out.println("bigger");
} else {
System.out.println("equals");
}
System.out.println(result);

这将随着输出变大而返回。 650 有点奇怪,因为数字小于 1000。

怎么样?我该如何改变它? (是的,数字需要采用文本格式)。

我想这样做:

int result = "650/65".compareTo("1050/50");
if(result < 0){
System.out.println("smaller");
} else if(result > 0){
System.out.println("bigger");
} else {
System.out.println("equals");
}

这会返回“650/65”大于“1050/50”,但实际上它更小。

编辑

我已经解决了这些情况,现在是这样的:

String maat = "650/65";
int subMaatB = 0;
int subMaatA = 0;

if(maat.contains("/")){
try{
subMaatA = Integer.parseInt(maat.substring(0, maat.lastIndexOf("/")));
subMaatB = Integer.parseInt(maat.substring(maat.lastIndexOf("/")+1));
} catch (NumberFormatException e){

}
}

boolean resultA = subMaatA >= 440;
boolean resultB = subMaatB >= 65;
boolean resultC = subMaatA <= 1050;
boolean resultD = subMaatB <= 50;
if(resultA && resultB && resultC && resultD){
System.out.println("BIGGER");
} else {
System.out.println("Smaller");
}

所以情况是,Maat 必须在 440/65 和 1050/50 的范围内。Maat 应该在我之前提到的范围之间。

最佳答案

"3".compareTo("2")"三".compareTo("二")

这意味着它们都是字符串比较。

String.compareTo()

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

this.charAt(k)-anotherString.charAt(k) If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:

如果您想将它们作为整数进行比较,则必须先将它们转换为整数。

关于java - 字符串比较没有返回正确的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18485375/

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