代码健壮性和性能都不怎么好,只是为了锻炼思维,嘻嘻😁
比大小
栗子:
1.1.2 > 1.0.2
1.1.1 > 1.1
1.1.0 = 1.1
public class Compare {
public static String maxStr(String str1,String str2){
String maxStr = null;
String [] s1 = str1.split("\\.");
String [] s2 = str2.split("\\.");
for (int i = 0; i < s2.length; i++) {
if (Integer.parseInt(s1[i]) > Integer.parseInt(s2[i])) {
maxStr = str1;
break;
} else if (Integer.parseInt(s1[i]) < Integer.parseInt(s2[i])) {
maxStr = str2;
break;
}
}
if (maxStr == null) {
if(s1.length==s2.length){
maxStr = str1;
}else {
for (int j = s2.length; j < s1.length; j++) {
if (Integer.parseInt(s1[j]) > 0) {
maxStr = str1;
break;
} else if ((j == s1.length - 1) && (maxStr == null)) {
maxStr = str1;
break;
}
}
}
}
System.out.println(maxStr);
return maxStr;
}
public static void main(String [] args){
String str1 = "1.1.2";
String str2 = "1.0.2";
String [] s1 = str1.split("\\.");
String [] s2 = str2.split("\\.");
if(s1.length>=s2.length) {
maxStr(str1, str2);
}else{
maxStr(str2, str1);
}
}
}
网友评论