如何以相反的顺序返回3个字符串?

时间:2016-11-23 17:52:45

标签: java arrays string reverse

我正在使用java编程,用户已经输入3个单词作为字符串,word1,word2和word 3.我的任务是首先大写所有单词,例如:run,roll,jump .....这些词应该变成RUN,ROLL,JUMP。问题是我必须反向排序,例如:JUMP,ROLL,RUN。我必须使用数组,对它们进行排序,然后返回单词,我该怎么做?这就是我所拥有的:

public static String reverseOrder(String word1, String word2, String word3) {
  int a = word1.length();
  int b = word2.length();
  int c = word3.length();

  String x;
  String y;
  String z;

  x = word1.toUpperCase(); 
  y = word2.toUpperCase();
  z = word3.toUpperCase();

//this should be the output
String[] r = reverseOrder(word1,word2,word3);
System.out.println(Arrays.toString(r));
}
}

1 个答案:

答案 0 :(得分:0)

    import java.util.Arrays;
    class MyStringTest{
      public static String[] reverseOrder(String word1, String word2,    
                                    String word3) {
        // int a = word1.length();
        // int b = word2.length();
        // int c = word3.length();

        // String x;
        // String y;
        // String z;

        String x = word1.toUpperCase(); 
        String y = word2.toUpperCase();
        String z = word3.toUpperCase();


        String[] r = new String[]{x, y, z};
        // return the array
        return r;  // check the return type


      }
      public static void main(String [] args){
        String [] r = reverseOrder("run", "roll", "jump");
        System.out.println(Arrays.toString(r));


      }

    }