Java第八次作业

1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)

 public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[] = { 10, 20, 30, 40, 50 };
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }

 

 2、将一个字符数组的值(neusoft)拷贝到另一个字符数组中。(知识点:数组复制)

复制代码
 public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] a = { "neusoft" };
        String[] b = new String[a.length];
        System.arraycopy(a, 0, b, 0, a.length);
        for (String string : b) {
            System.out.println(string);
        }
复制代码

 

 3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)

复制代码
 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型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)

复制代码
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个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。

复制代码
 public static void main(String[] args) {
        // TODO Auto-generated method stub

        int[] a = new int[] { 18, 25, 7, 36, 1, 2, 89, 63 };
        int max = a[0];
        int min = 0;
        for (int i = 0; i < a.length; i++) {
 
            if (a[i] > max) {
                max = a[i];
                min = i;
            }
 
        }
        System.out.println("下标:" + min);
        System.out.println("最大值:" + max);
    }
复制代码

 

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

复制代码
 public static void main(String[] args) {
       // TODO Auto-generated method stub
          int []a={1,2,3,4,5,6,7,8,9};
            for(int x=0;x<9;x++){
                System.out.print(a[x]+" ");
            }
            System.out.println();
            for (int y=8;y>=0;y--){
                System.out.print(a[y]+" ");
            }
复制代码

 

 7. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问)

复制代码
public static void main(String[] args) {
        // TODO Auto-generated method stub

         int[] a = { 1, 3, 7, 6, 1, 2, 9, 3 };
         
         
            for (int i = 0; i < a.length; i++) {
       for (int j = 0; j < a.length; j++) {
           if(a[i]==a[j]&&i!=j){
               a[j]=0;
           }
            
         
    }
         
    }
            for (int i = 0; i < a.length; i++) {
                System.out.println(a[i]);
                 
       }
复制代码

 

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

复制代码
public class test1 {
     public static void main(String[] args){
            int []a={ -10,2,3,246,-100,0,5};
            int max = a[0],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

复制代码
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之间),输出,排序后,再输出

复制代码
 public static void main(String[] args) {
        // TODO Auto-generated method stub

        int l[] = new int[10];
        Random x = new Random();
 
        for (int i = 0; i < l.length; i++) {
            l[i] = x.nextInt(101);
        }
        for (int i = 0; i < l.length; i++) {
            System.out.println(l[i]);
        }
        System.out.println("排序后:  ");
        Arrays.sort(l);
        for (int i = 0; i < l.length; i++) {
            System.out.println(l[i]);
        }
复制代码

posted @ 2021-04-27 20:19  张思文04  阅读(30)  评论(0编辑  收藏  举报