public class Demo2 {
public static void main(String[] args) {
String s = "91 27 46 38 50";
/*
*思路
*1、用split切割 成字符数组
*2、定义一个和字符串等长度的数组,用来接收字符转换成的数字
*
*/
String [] sarr = s.split(" ");
int arr[] = new int[sarr.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(sarr[i]);
}
Arrays.sort(arr);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
if(i == arr.length - 1) {
sb.append(arr[i]);
}else {
sb.append(arr[i] + " ");
}
}
System.out.println(sb);
}
} |
|