Java學習筆記6(迴圈和數組練習題)

來源:互聯網
上載者:User

標籤:arch   i+1   system   --   列印   bubble   void   ==   選擇排序   

1.輸出100到1000的水仙花數:

public class LoopTest{    public static void main(String[] args){        int bai = 0;        int shi = 0;        int ge = 0;        for(int i = 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.列印所有字母:

public class LoopTest{    public static void main(String[] args){        char lower = ‘a‘;        char upper = ‘A‘;        for(int i = 0 ; i < 26 ; i++){            System.out.println(lower+"   "+upper);            lower++;            upper++;        }    }}

3.列印99乘法表:

public class LoopTest{    public static void 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.按格式遍曆數組:

public class ArrayTest{    public static void main(String[] args){        int[] arr1 = {4,6,7,1};        int[] arr2 = {1,2,3,9,10};        printArray(arr1);        printArray(arr2);    }    public static void printArray(int[] arr){        System.out.print("[");        for(int i = 0 ; i <arr.length ; i++){            if( i == arr.length-1){                System.out.println(arr[i]+"]");            }else{                System.out.print(arr[i]+",");            }        }    }}

5.數組元素逆序(注意:不是反向遍曆):

public class ArrayTest{    public static void main(String[] args){        int[] arr = {1,2,3,4,7,8,9};        reverse(arr);        printArray(arr);    }    public static void reverse(int[] arr){        for(int min=0,max = arr.length-1; min<max ; min++,max--){            int temp = arr[min];            arr[min] = arr[max];            arr[max] = temp;        }    }     public static void printArray(int[] arr){        System.out.print("[");        for(int i = 0 ; i <arr.length ; i++){            if( i == arr.length-1){                System.out.println(arr[i]+"]");            }else{                System.out.print(arr[i]+",");            }        }    }}

6.選擇排序:

public class ArrayTest{    public static void main(String[] args){        int[] arr = {3,1,5,7,8,9,2};        selectSort(arr);        printArray(arr);    }    public static void selectSort(int[] arr){        for(int i = 0; i < arr.length-1; i++){            for(int j = i+1; j<arr.length; j++){                if(arr[i]>arr[j]){                    int temp = arr[i];                    arr[i] = arr[j];                    arr[j] = temp;                }            }        }    }    public static void printArray(int[] arr){        System.out.print("[");        for(int i = 0 ; i <arr.length ; i++){            if( i == arr.length-1){                System.out.println(arr[i]+"]");            }else{                System.out.print(arr[i]+",");            }        }    }}

7.冒泡排序:

public class ArrayTest{    public static void main(String[] args){        int[] arr = {3,1,5,7,8,9,2};        bubbleSort(arr);        printArray(arr);    }    public static void bubbleSort(int[] arr){        for(int i = 0; i<arr.length-1 ;i++){            for(int j = 0; j<arr.length-i-1; j++){                if(arr[j]>arr[j+1]){                    int temp = arr[j];                    arr[j] = arr[j+1];                    arr[j+1] = temp;                }            }        }    }    public static void printArray(int[] arr){        System.out.print("[");        for(int i = 0 ; i <arr.length ; i++){            if( i == arr.length-1){                System.out.println(arr[i]+"]");            }else{                System.out.print(arr[i]+",");            }        }    }}

8.折半尋找法(注意必須是有序的數組):

public class ArrayTest{    public static void main(String[] args){        int[] arr = {1,3,5,7,9,11};        int index = binarySearch(arr, 7);        System.out.println(index);    }    public static int binarySearch(int[] arr,int key){        int min = 0;        int max = arr.length-1;        int mid = 0;        while( min<=max ){            mid = (min+max)/2;            if(key>arr[mid]){                min = mid+1;            }else if(key<arr[mid]){                max = mid-1;            }else{                return mid;            }        }        return -1;    }}

 

Java學習筆記6(迴圈和數組練習題)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.