如果else语句不是数组,如何使用字母顺序对任意三个变量值字符串值进行排序?因此,如果我更改“ w”变量的值,它将仍然有效。
总体而言,我目前遇到的问题甚至是能够将分配给变量的任何字符串值都按字母顺序排列。我想知道是否有人可以帮助我?
如果您问为什么不使用数组是因为我正在处理的事情我无法使用它们。
下面的代码表示我在想什么。
String w = "York";
String k = "Oxford";
String t = "Sam";
if ((w.compareTo(k) <= 0) && (k.compareTo(t) <= 0)) {
System.out.println(k);
System.out.println(w);
System.out.println(t);
} else if ((w.compareTo(t) <= 0) && (k.compareTo(t) <= 0)) {
System.out.println(k);
System.out.println(w);
System.out.println(t);
} else if ((t.compareTo(w) <= 0) && (w.compareTo(k) <= 0)) {
System.out.println(t);
System.out.println(w);
System.out.println(k);
} else if ((t.compareTo(k) <= 0) && (k.compareTo(w) <= 0)) {
System.out.println(w);
System.out.println(t);
System.out.println(k);
} else if ((k.compareTo(w) <= 0) && (w.compareTo(t) <= 0)) {
System.out.println(w);
System.out.println(k);
System.out.println(t);
} else {
System.out.println(k);
System.out.println(t);
System.out.println(w);
}
}
}
答案 0 :(得分:5)
为了节省代码量和比较调用次数,我会像插入排序那样做:
String w = "York";
String k = "Oxford";
String t = "Sam";
String s1, s2, s3;
// insert first 2 values in correct order
if (w.compareTo(k) <= 0) {
s1 = w;
s2 = k;
} else {
s1 = k;
s2 = w;
}
// insert 3rd value in correct place
if (s2.compareTo(t) <= 0) {
s3 = t;
} else {
s3 = s2;
if (s1.compareTo(t) <= 0) {
s2 = t;
} else {
s2 = s1;
s1 = t;
}
}
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
输出
Oxford
Sam
York
答案 1 :(得分:0)
您可以使用收藏集吗? 在这种情况下,您可以使用列表并按以下方式进行操作,
List<String> list = new ArrayList();
list.add("York");
list.add("Oxford");
list.add("Sam");
Collections.sort(list);
答案 2 :(得分:0)
以下代码根据需要解决了该问题,但是请注意,它非常原始,难以阅读且极难扩展(这是不良代码的一个很好的示例,不应在生产中使用):
String w = "York";
String k = "Oxford";
String t = "Sam";
if (w.compareTo(k) <= 0 && w.compareTo(t) <= 0 && k.compareTo(t) <= 0) {
System.out.println(w);
System.out.println(k);
System.out.println(t);
} else if (w.compareTo(k) <= 0 && w.compareTo(t) <= 0 && t.compareTo(k) <= 0) {
System.out.println(w);
System.out.println(t);
System.out.println(k);
} else if (k.compareTo(w) <= 0 && k.compareTo(t) <= 0 && w.compareTo(t) <= 0) {
System.out.println(k);
System.out.println(w);
System.out.println(t);
} else if (k.compareTo(w) <= 0 && k.compareTo(t) <= 0 && t.compareTo(w) <= 0) {
System.out.println(k);
System.out.println(t);
System.out.println(w);
} else if (w.compareTo(k) <= 0) {
System.out.println(t);
System.out.println(w);
System.out.println(k);
} else {
System.out.println(t);
System.out.println(k);
System.out.println(w);
}
输出
Oxford
Sam
York
答案 3 :(得分:-1)
如果仅需要else语句,请尝试此
String w = "York";
String k = "Oxford";
String t = "Sam";
char wCHar = w.toLowerCase().charAt(0);
char kCHar = k.toLowerCase().charAt(0);
char tCHar = t.toLowerCase().charAt(0);
if (wCHar < kCHar && kCHar < tCHar) {
System.out.println(w);
System.out.println(k);
System.out.println(t);
} else if (wCHar < tCHar && tCHar < kCHar) {
System.out.println(w);
System.out.println(t);
System.out.println(k);
} else if (kCHar < wCHar && wCHar < tCHar) {
System.out.println(k);
System.out.println(w);
System.out.println(t);
} else if (kCHar < tCHar && tCHar < wCHar) {
System.out.println(k);
System.out.println(t);
System.out.println(w);
} else if (tCHar < wCHar && wCHar < kCHar) {
System.out.println(t);
System.out.println(w);
System.out.println(k);
} else if (tCHar < kCHar && kCHar < wCHar) {
System.out.println(t);
System.out.println(k);
System.out.println(w);
}