2016-11-23 48 views
-1

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

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)); 
} 
} 
+0

使用谷歌找到如何排序在Java中的数组。然后编写自己的比较器,或重新使用现有的比较器:http://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#reverseOrder-- –

+0

您可以尝试反转单个字符串首先,你可以为任意数量的字符串做 – developer

+0

等等,我甚至都不明白这个问题。 JUMP,ROLL,RUN按自然顺序排序。不是相反的顺序。如果您只是想按照插入顺序的相反顺序显示这些单词,那么您只需要从头到尾循环访问数组。 –

回答

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)); 


     } 

    } 
+0

不要忘记导入java.util.Arrays – Shiba

+0

谢谢,它的工作原理 –

相关问题