Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

nhậo mảng trong java

Discussion in 'Xây dựng ứng dụng server-client desktop' started by curuaasv, 24/2/11.

  1. curuaasv

    curuaasv New Member

    có ai biết cách nhập mảng các số trong java không? chỉ cho mình với
     
  2. LTV_htrieu

    LTV_htrieu New Member

    Ðề: nhậo mảng trong java

    PHP:
    import java.util.*;
    class 
    in_out_array
    {
        public static 
    void main(String args[])
        {
                
    Scanner in=new Scanner(System.in);
                
    int n=0;
                
    double max=0;
                try{
                        
    System.out.println("nhap so phan tu mang: ");
                        
    n=in.nextInt();
                }catch(
    InputMismatchException e)
                {
                    
    System.out.println("sai kieu");
                }

                        
    double[] d=new double[n];
                        for(
    int i=0;i<n;i++)
                        {
                            
    System.out.println("Nhap Phan tu thu "+i);
                            
    d[i]=in.nextDouble();
                        }
                
                
    //xuat mang:
                
    for(int i=0;i<d.length;i++)
                {
                    
    System.out.println("Phan tu thu "+i+"la :"+d[i]);
                }
                
    //xuat phan tu lon nhat
                /*for(int i=0;i<d.length;i++)
                {
                    if(d[i]>max)
                        max=d[i];
                }
                System.out.println("phan tu lon nhat la: "+max);
                for(int i=0;i<d.length;i++)
                {
                    if(d[i]==max)
                    System.out.println("Phan tu  lon nhat dau tien thu: "+i+" gia tri :"+max);
                    break;
                }
                //tim phan tu dau tien trong mang voi gia tri x
                /*System.out.print("nhap phan tu can tim: ");
                double x=in.nextDouble();
                for(int i=0;i<d.length;i++)
                {
                    if(d[i]==x)
                    {
                        System.out.println("la phan tu dau tien thu "+i);
                        break;
                    }
                }*/
                // sap xep mang tang dan
                
    double tam=0;
                for(
    int j=0;j<n-1;j++)
                {
                    for(
    int i=j+1;i<n;i++)
                    {
                        if(
    d[j]>d[i])
                        {
                            
    tam=d[j];
                            
    d[j]=d[i];
                            
    d[i]=tam;
                        }
                    }
                }
                
    System.out.println("sap xep tang dan");
                for(
    int i=0;i<d.length;i++)
                {
                    
    System.out.println("Phan tu thu "+i+"la :"+d[i]);
                }
        }
    }

    tham khảo nha.
     
  3. curuaasv

    curuaasv New Member

    Ðề: nhậo mảng trong java

    thank ban rất nhiều
     

Chia sẻ trang này

Loading...