2016-11-23 54 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

謝謝,它的工作原理 –