Java bubble, average value of string output

Source: Internet
Author: User

Java coding bubbleAlgorithm, And calculate the average value of the array, and then output it as a string.

 

Public class variable {/*** @ Param ARGs */public static void main (string [] ARGs) {int A [] = {, 3 }; int COUNT =. length; string B = new string (); fun Ling = new fun (); B = Ling. AVG (a); // system. out. println ("B =" + B + "\ n"); // system. out. printf ("avg1 = % 5s \ n", B); system. out. println (B); Ling. bubble (a); system. out. println ("Bubble Sorting after calling:"); system. out. printf ("["); For (int K = 0; k <count; k ++) {system. out. printf (A [k] + "");} system. out. printf ("]") ;}} class Fun {Public String AVG (int A []) {int sum = 0; double avge = 0.0; int I = 0; int COUNT =. length; string avge1 = new string (); // system. out. printf ("Count = % d \ n", count); for (I = 0; I <count; ++ I) {sum + = A [I];} avge = sum/(count * 1.0); // system. out. printf ("sum =" + sum + "\ n"); system. out. printf ("AVG = %. 3f \ n ", avge); // avge1 = avge. tostring (); avge1 = string. format ("%. 3f ", avge); // string output format, three decimal places // avge1 = avge +" "; return avge1;} public void bubble (int A []) {int COUNT =. length; int temp = 0; system. out. println ("before Bubble Sorting:"); system. out. printf ("["); For (int K = 0; k <count; k ++) {system. out. printf (A [k] + "");} system. out. printf ("]"); system. out. printf ("\ n"); For (INT I = 0; I <count; I ++) for (Int J = I + 1; j <count; j ++) {if (a [I]> A [J]) {temp = A [I]; A [I] = A [J]; A [J] = temp ;}} system. out. println ("after Bubble Sorting:"); system. out. printf ("["); For (int K = 0; k <count; k ++) {system. out. printf (A [k] + "");} system. out. printf ("]"); system. out. printf ("\ n ");}}

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.