Java Learning Note 6 (loops and array exercises)

Source: Internet
Author: User

1. Output the number of daffodils from 100 to 1000:

 Public classlooptest{ Public Static voidMain (string[] args) {intBai = 0; intShi = 0; intGE = 0;  for(inti = 100; i < 1000; i++) {Bai= i/100; Shi= i/10%10; GE= i%10; if(Bai*bai*bai+shi*shi*shi+ge*ge*ge = =i)            {System.out.println (i); }        }    }}

2. Print all letters:

 Public class looptest{    publicstaticvoid  main (string[] args) {        Char lower = ' a ';         char upper = ' A ';          for (int i = 0; i <; i++) {            System.out.println (lower+ "   " +Upper);            Lower+ +;            Upper+ +;     }}}

3. Print the 99 multiplication table:

 Public class looptest{    publicstaticvoid  main (string[] args) {        for (int i = 1; I <=9; i++) {            for (int j = 1; j<=i; j + +) {
    system.out.print (J+ "*" +i+ "=" +j*i+ "    ");            }            System.out.println ();     }}}

4. Iterate through the array as a format:

 Public classarraytest{ Public Static voidMain (string[] args) {int[] arr1 = {4,6,7,1}; int[] arr2 = {1,2,3,9,10};        PrintArray (ARR1);    PrintArray (ARR2); }     Public Static voidPrintArray (int[] arr) {System.out.print ("[");  for(inti = 0; I <arr.length; i++){            if(i = = Arr.length-1) {System.out.println (Arr[i]+"]"); }Else{System.out.print (Arr[i]+","); }        }    }}

5. Reverse the array elements (note: not backward traversal):

 Public classarraytest{ Public Static voidMain (string[] args) {int[] arr = {1,2,3,4,7,8,9};        Reverse (arr);    PrintArray (arr); }     Public Static voidReverseint[] arr) {         for(intMin=0,max = arr.length-1; Min<max; min++,max--){            inttemp =Arr[min]; Arr[min]=Arr[max]; Arr[max]=temp; }    }      Public Static voidPrintArray (int[] arr) {System.out.print ("[");  for(inti = 0; I <arr.length; i++){            if(i = = Arr.length-1) {System.out.println (Arr[i]+"]"); }Else{System.out.print (Arr[i]+","); }        }    }}

6. Select Sort:

 Public classarraytest{ Public Static voidMain (string[] args) {int[] arr = {3,1,5,7,8,9,2};        Selectsort (arr);    PrintArray (arr); }     Public Static voidSelectsort (int[] arr) {         for(inti = 0; i < arr.length-1; i++){             for(intj = i+1; j<arr.length; J + +){                if(arr[i]>Arr[j]) {                    inttemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; }            }        }    }     Public Static voidPrintArray (int[] arr) {System.out.print ("[");  for(inti = 0; I <arr.length; i++){            if(i = = Arr.length-1) {System.out.println (Arr[i]+"]"); }Else{System.out.print (Arr[i]+","); }        }    }}

7. Bubble Sort:

 Public classarraytest{ Public Static voidMain (string[] args) {int[] arr = {3,1,5,7,8,9,2};        Bubblesort (arr);    PrintArray (arr); }     Public Static voidBubblesort (int[] arr) {         for(inti = 0; i<arr.length-1; i++){             for(intj = 0; j<arr.length-i-1; J + +){                if(arr[j]>arr[j+1]){                    inttemp =Arr[j]; ARR[J]= Arr[j+1]; Arr[j+1] =temp; }            }        }    }     Public Static voidPrintArray (int[] arr) {System.out.print ("[");  for(inti = 0; I <arr.length; i++){            if(i = = Arr.length-1) {System.out.println (Arr[i]+"]"); }Else{System.out.print (Arr[i]+","); }        }    }}

8. Binary Lookup method (note must be an ordered array):

 Public classarraytest{ Public Static voidMain (string[] args) {int[] arr = {1,3,5,7,9,11}; intindex = BinarySearch (arr, 7);    SYSTEM.OUT.PRINTLN (index); }     Public Static intBinarySearch (int[] arr,intkey) {        intMin = 0; intmax = Arr.length-1; intMID = 0;  while(min<=max) {Mid= (Min+max)/2; if(key>Arr[mid]) {min= Mid+1; }Else if(key<Arr[mid]) {Max= Mid-1; }Else{                returnmid; }        }        return-1; }}

Java Learning Note 6 (loops and array exercises)

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.