Java怎么同时显示:有一列乱序的字符a,c,u,b,e,p,f,z排序并按照英文字母表的逆序输出
publicstaticvoidmain(String[]args){String[]words=newString[]{"a","c","u","b","e","p",...
public static void main(String[] args) {
String[] words = new String[]{"a","c","u","b","e","p","f","z"};
for(int i=words.length-1; i>=0; i--) {
for(int j=0; j<i; j++) {
if(words[j].compareTo(words[j+1]) < 0) {
String temp = words[j];
words[j] = words[j+1];
words[j+1] = temp;
}
}
}
for(int i=0; i<words.length; i++) {
System.out.print(words[i] + " ");
}
}
}
这个怎么同时显示原字符序列,升序排序后,逆序输出? 展开
String[] words = new String[]{"a","c","u","b","e","p","f","z"};
for(int i=words.length-1; i>=0; i--) {
for(int j=0; j<i; j++) {
if(words[j].compareTo(words[j+1]) < 0) {
String temp = words[j];
words[j] = words[j+1];
words[j+1] = temp;
}
}
}
for(int i=0; i<words.length; i++) {
System.out.print(words[i] + " ");
}
}
}
这个怎么同时显示原字符序列,升序排序后,逆序输出? 展开
1个回答
展开全部
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String[] words = new String[] { "a", "c", "u", "b", "e", "p", "f", "z" };
System.out.print("old str : ");
for (String s : words) {
System.out.print(s + " ");
}
for (int i = words.length - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (words[j].compareTo(words[j + 1]) < 0) {
String temp = words[j];
words[j] = words[j + 1];
words[j + 1] = temp;
}
}
}
System.out.print("\nnew str : ");
for (int i = 0; i < words.length; i++) {
System.out.print(words[i] + " ");
}
}
}
更多追问追答
追问
怎么按字母顺序排序?
追答
new str 的结果就是字母顺序逆序啊
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询