import java.util.Arrays;
//"34 9 -7 12 67 25"要求对这个字符串中的数值进行从小到大的排序,
//然后生成一个数值有序的字符串。
public class String1 {
public static void main(String[] args) {
String str = "23 -21 2 12 -2";
String[] arr = str.split(" ");
// String[] arr={"23","-21","2","12","-2"};
int[] arr1 = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
int num = Integer.parseInt(arr[i]);
arr1[i] = num;
}
for (int i = 1; i < arr1.length - 1; i++) {
for (int j = 0; j < arr1.length - i; j++) {
if (arr1[j] > arr1[j + 1]) {
int temp = arr1[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = temp;
}
}
}
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i]+" ");
} |
|