Sorts the characters in the string entered by the keyboard.
Example: "DACGEBF"
Result: "ABCDEFG"
Analysis: 1, first to convert the characters to an array, and to sort:
A: Directly let them be compared in the form of characters
B: Bubble Sort or select sort all can
2, the array is traversed, and converted to a string, and output
3, use the method to operate:
A: return type: String
B: Parameter list: String
1 ImportJava.util.Scanner;2 Public classArrayTest3 {3 4 Public Static voidMain (string[] args) {5 //Create keyboard input6Scanner sc =NewScanner (system.in);7System.out.println ("Please enter the string you want to sort:");8String str =sc.nextline ();9 Ten //Calling Methods OneString result =Sort (str); ASystem.out.println ("The sorted string is:" +result); - - } the //To create a sorting method: - Public Staticstring Sort (String str) { - //Convert a string to an array - Char[] ch =Str.tochararray (); + //sorting the array, using the bubble sort method - for(intx = 0; x < ch.length-1; X + +){ + for(inty = 0; Y < ch.length-1-X; y++){ A //Sort at if(Ch[y] > ch[y+1]){ - Chartemp =Ch[y]; -Ch[y] = ch[y+1]; -CH[Y+1] =temp; - } - } in } - //the array is converted to a string and returned to returnstr =string.valueof (CH); + } -}
Java12-7 sort of case