String 類別 (class) 有 compareTo() 方法 (method)。
若str1 > str2 回傳true
str2 > str1 回傳false
相等回傳0
public class SequenceString { public static void main(String args[]){ String string1 = "A"; String string2 = "To"; String string3 = "Z"; String string1Out = "\"" + string1 + "\""; String string2Out = "\"" + string2 + "\""; String string3Out = "\"" + string3 + "\""; if(string1.compareTo(string3) < 0){ System.out.println(string1Out + " is less than " + string3Out); }else{ if(string1.compareTo(string3) > 0){ System.out.println(string1Out + " is greater than " + string3Out); }else{ System.out.println(string1Out + " is equal to " + string3Out); } } if(string2.compareTo(string1) < 0){ System.out.println(string2Out + " is less than " + string1Out); }else{ if(string2.compareTo(string1) > 0){ System.out.println(string2Out + " is greater than " + string1Out); }else{ System.out.println(string2Out + " is equal to " + string1Out); } } } }
沒有留言:
張貼留言