Java Alphabetizing Strings

后端 未结 7 1253
悲哀的现实
悲哀的现实 2021-01-19 04:27

I have a project for class were I need to get 4 different strings inputted and then output them in alphabetical order.

So far I have this:

String wd1         


        
7条回答
  •  滥情空心
    2021-01-19 04:54

    boolean swapped = false;
    do {
      swapped = false;
      if (w2.compareTo(w1) < 0) {
        String tmp = w2;
        w2 = w1;
        w1 = tmp;
        swapped = true;
      }
      if (w3.compareTo(w2) < 0) {
        String tmp = w3;
        w3 = w2;
        w2 = tmp;
        swapped = true;
      }
      if (w4.compareTo(w3) < 0) {
        String tmp = w4;
        w4 = w3;
        w3 = tmp;
        swapped = true;
      }
    } while (swapped)
    
    System.out.println(w1);
    System.out.println(w2);
    System.out.println(w3);
    System.out.println(w4);
    

提交回复
热议问题