Minh họa cách xây dựng hàm trong java


import java.util.Scanner;

public class demo {

            public static void main(String[] args) {

            int a[] = new int[100];

            nhap(a,10);

            xuat(a,10);

            chanle(a,10);

            dayNT(a,10);

            }

            //Nhap mang

            public static void nhap(int[] a, int n){

                        Scanner objSc = new Scanner(System.in);

                        for(int i =0; i<n;i++){

                                    System.out.print("Nhap a"+i);

                                    a[i] = objSc.nextInt();

                        }

            }

            //xuat mang

            public static void xuat(int[] a, int n){

                        for(int i=0;i<n;i++){

                                    System.out.print(" "+a[i]);

                        }

            }

            //Kiem tra so nguyen to

            public static int checkSoNT(int n){

                        int dem=0;

                        for(int i =1; i<=n;i++){

                                    if(n%i==0)

                                                dem++;

                        }

                        if(dem==2)

                                    return 1;

                        else return 0;

            }

            //Ham kiem tra so chinh phuong

            public static void soCP(int n){

                       

            }

            //Ham in ra day so nguyen to

            public static void dayNT(int[] a, int n){

                        int[] nguyento = new int[n];

                        int k=0;

                        for(int i=0; i<n;i++){

                                    if(checkSoNT(a[i])==1){

                                                nguyento[k]=a[i];

                                                k++;

                                    }

                        }

                        System.out.println("DAY SO NGUYEN TO");

                        for(int i=0;i<k;i++){

                                    System.out.print(" "+nguyento[i]);

                        }

            }

            //mang chan/le

            public static void chanle(int[] a, int n){

                        int[] chan = new int[n];

                        int c=0,l=0;

                        int sumchan=0,sumle=0;

                        int[] le = new int[n];

                        for(int i =0;i<n;i++){

                                    if(a[i]%2==0){

                                                chan[c]=a[i];

                                                sumchan+=a[i];

                                                c++;

                                    }

                                    else{

                                                le[l]=a[i];

                                                sumle +=a[i];

                                                l++;

                                    }

                        }

                        System.out.println();

                        System.out.println("MANG SO CHAN");

                        for(int i=0;i<c;i++)

                                    System.out.print(" "+chan[i]);

                                    System.out.println();

                                    System.out.println("Tong chan:"+sumchan);

                       

                        System.out.println();

                        System.out.println("MANG SO LE");

                        for(int i =0;i<l;i++)

                                    System.out.print(" "+le[i]);

                                    System.out.println();

                                    System.out.println("Tong le:"+sumle);

            }

}

Trương Đình Huy