【问题标题】:How do I return 3 Strings in reverse order?如何以相反的顺序返回 3 个字符串?
【发布时间】:2016-11-23 17:52:45
【问题描述】:

我正在使用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));
}
}

【问题讨论】:

  • 使用 google 查找如何在 Java 中对数组进行排序。然后编写自己的比较器,或者重用现有的:docs.oracle.com/javase/8/docs/api/java/util/…
  • 您可以先尝试反转单个字符串,然后您可以尝试任意数量的字符串
  • 等等,我什至不明白这个问题。 JUMP、ROLL、RUN 按自然顺序排序。不倒序。如果您只想以与插入顺序相反的顺序显示单词,那么您只需从数组的末尾循环到开头。
  • @JBNizet 我相信这里的“order”是指参数的顺序。

标签: java arrays string reverse


【解决方案1】:
    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));


      }

    }

【讨论】:

  • 别忘了导入 java.util.Arrays
猜你喜欢
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 2016-06-02
  • 2018-12-25
  • 1970-01-01
相关资源
最近更新 更多