您可以使用 arrays.sort 获取用户输入并进行相应排序
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class SortMyNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
String strUserInput = "";
String strOp = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
do {
System.out
.println("...Enter no.s or Type End to terminate the program...");
try {
strUserInput = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (!strUserInput.equalsIgnoreCase("end")
&& strUserInput.contains(",")) {
System.out.println(" Entered No.s are ..." + strUserInput);
System.out.println("Enter no.s...");
String strArr[] = strUserInput.split(",");
double iArr[] = new double[strArr.length];
int i = 0;
// Arrays.sort(strArr);
for (String s : strArr) {
iArr[i] = Double.parseDouble(s);
i++;
}
Arrays.sort(iArr);
for (double j : iArr) {
strOp += String.valueOf(j) + ",";
}
System.out.println(" Sorted No are " + strOp);
} else {
// System.out.println("Invalid i/p terminating...");
}
strOp = strOp.substring(0, strOp.length() - 1);
} while (!strUserInput.equalsIgnoreCase("end"));
}
}