Java12-7 sort of case

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.