【发布时间】: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