我目前在我的第一個學期。我有一個項目需要我建立一個用戶輸入3個字的程序,按字母順序排序並輸出中間字。我已經做了一些搜索,似乎只是返回排序2個單詞的結果。我到目前爲止有代碼來獲得用戶輸入,但我完全失去了如何按字母順序排序以及如何提示用戶輸入三個字符串。請耐心等待,因爲我對編程非常陌生。如果任何人都可以向我提供任何建議或着手整理這些最好的或最簡單的方式,我將不勝感激提示用戶輸入,然後按字母順序排序?
import java.util.Scanner; //The Scanner is in the java.util package.
public class MiddleString {
public static void main(String [] args){
Scanner input = new Scanner(System.in); //Create a Scanner object.
String str1, str2, str3;
System.out.println("Please enter one word words : "); //Prompt user to enter one word
str1=input.next(); //Sets "str1" = to first word.
str2=input.next(); //Sets "str2" = to second word.
str3=input.next(); //Sets "str3" = to third word.
System.out.println("The middle word is "); // Outputs the middle word in alphabetical order.
}
}
請幫幫忙!
提示:''''對象有一個['compareTo()'](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo%28java.lang .String%29)方法。 – Undefined
*必須*您使用單獨的變量?理想情況下,將使用List(或者甚至數組)來收集這樣的輸入(因爲集合是[trivially sorted](http://stackoverflow.com/questions/708698/how-to-sort-list-alphabetically))。 – user2864740