字符串精練——將一個隨機字符串中的所有字符升序排列,並倒序打印

將一個隨機字符串中的所有字符升序排列,並倒序打印

import java.util.*;
public class Main {
		public static void main(String[] args) {
			Scanner sc = new Scanner(System.in);
			System.out.print("隨機輸入字符串:");
			String s = sc.next();
			sort(s);
		}
		public static void sort(String s) {
			char [] c = s.toCharArray() ;
			Arrays.sort(c);
			System.out.println("升序排序:");
			for(char e : c) {
				System.out.print(e+" ");
			}
			System.out.println();
			 System.out.println("輸入字符串倒序排列爲:");
		        for (int i = c.length - 1; i >= 0; i--) {
		            System.out.print(c[i] + " ");
		        }
			
		}
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章