思路如下:
1,录入字符串
2,使用正则表达式分割
3,转换为int数组
4,排序
5,拼接,代码如下:
import java.util.Arrays;
import java.util.Scanner;
public class Demo8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();// 录入字符串
String[] strs = str.split(",");// 使用正则表达式进行分割
int[] is = new int[strs.length];
for (int i = 0; i < strs.length; i++) {// 遍历String数组,赋值给int数组
is[i] = Integer.parseInt(strs[i]);
}
Arrays.sort(is);// 使用数组工具类进行排序,也可以自己使用冒泡或选择排序来进行排序
StringBuffer sb = new StringBuffer();
for (int i = 0; i < is.length; i++) {// 遍历进行拼接
if (i == is.length - 1) {
sb.append(is[i]);
} else {
sb.append(is[i] + ",");
}
}
System.out.println(sb.toString());
}
import java.util.*;
public class Test{
public static void main(String args[]){
System.out.print("Please input the numbers: ");
String input = new Scanner(System.in).nextLine().trim();
StringBuffer sb = new StringBuffer();
sb.append(input);
String reversedStr = sb.reverse().toString();
System.out.println("Reversed string is: " + reversedStr );
}
}
yaoyong henduo dongxi buhao shuo