第八次作业

1、编写一个简单程序,要求数组长度为5,分别赋值1020304050,在控制台输出该数组的值

 

package bbb;

public class Test1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int []a=new int[5];
        a[0]=10;
        for (int i = 1; i < a.length; i++) {
            a[i]=a[i-1]+10;
        }
        for (int j : a) {
            System.out.println(j);
        }
    }

}

 

2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中

 

package bbb;

import java.util.Arrays;

public class Test2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String []a={"neusofteducation"};
        String []b=new String[a.length];
        for (int i = 0; i < b.length; i++) {
            b[i]=a[i];
        }
        System.out.println(Arrays.toString(b));
    }

}

 

3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值

 

package bbb;

public class Test3 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int []a={1,6,2,3,9,4,5,7,8};
        for (int i = 0; i < a.length-1; i++) {
            for (int j = 0; j < a.length-1-i; j++) {
                if(a[j]>a[j+1]){
                    int temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        for (int i : a) {
            System.out.println(i);
        }
    }

}

 

4、 输出一个double型二维数组(长度分别为54,值自己设定)的值

 

package bbb;

public class Test4 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double [][]a=new double[5][4];
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                a[i][j]=j;
            }            
        }
        
        System.out.println("矩阵是");
        
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j]+"     ");
            }
            System.out.println();
        }
        

    }
}

 

5、 在一个有8个整数(18257361328963)的数组中找出其中最大的数及其下标

 

package bbb;

public class Test5 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int []a={18,25,7,36,13,2,89,63};
        int max=a[0];
        int maxidx=0;
        for (int i = 0; i < a.length; i++) {
            if(a[i]>max){
                max=a[i];
                maxidx=i;
            }
        }
        System.out.println("最大值是"+max+"它的下标是"+maxidx);
    }

}

 

 6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)

package bbb;
public class Test6 {
/**
     * @param args
     */

public static void main(String[] args) {
    // TODO Auto-generated method stub
        int a[] =new int[10];
        for (int i = 0; i < a.length; i++) {
            a[i] =i;
        }
        for (int i : a) {
            System.out.print(i +" ");
        }
        System.out.println();
        for (int i = 0; i < a.length; i++) {
            for (int j = i; j < a.length; j++) {
                int temp =a[i];
                a[i] =a[j];
                a[j] =temp;
            }
        }
        for (int i : a) {
            System.out.print(i +" ");
        }
    }
}

8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)

package bbb;

public class Test8 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[] = { -10, 2, 3, 246, -100, 0, 5 };
        int max = a[0];
        int min = a[0];
        double sum = 0;
        for (int i = 1; i < a.length; i++) {
            if (a[i] > max) {
                max = a[i];
            }
            if (a[i] < min) {
                min = a[i];
            }
        }
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }
        System.out.println("最大值为:" + max);
        System.out.println("最小值为:" + min);
        System.out.println("平均值为:" + sum / a.length);
    }

}

9、使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21

package bbb;

public Test9{

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[] = new int[20];
        a[0] = 1;
        a[1] = 1;
        for (int i = 2; i < a.length; i++) {
            a[i] = a[i - 1] + a[i - 2];
        }
        for (int i = 0; i < 8; i++) {
            System.out.println(a[i]);
        }
    }

}

10、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出

package bbb;

import java.util.Arrays;
import java.util.Random;

public Test10 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int []a=new int[10];
        Random x=new Random();
        for(int i=0;i<10;i++)
        {
            a[i]=x.nextInt(101);
        }
        System.out.println("输出为");
        for(int h:a)
        {System.out.print(" "+h);}
        System.out.println();
        System.out.println("排序后输出为");
        int m=a[0];
        Arrays.sort(a);
        for(int h:a)
        {System.out.print(" "+h);}
    }

}

 

posted @ 2021-04-25 21:53  刘源丰  阅读(39)  评论(0编辑  收藏  举报