第八周作业

1、 编写一个简单程序,要求数组长度为5,分别赋值1020304050,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]?

 

package cn.test;

import java.util.Scanner;

public class Work1 {

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

}

 

2. 将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]?

 

package cn.test;

public class Work2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char[] a = { 'n', 'e', 'u', 's', 'o', 'f', 't', 'e', 'd', 'u', 'c',
                'a', 't', 'i', 'o', 'n' };
        char[] b = new char[a.length];
        for (int i = 0; i < b.length; i++) {
            b[i] = a[i];
        }
        System.out.println(b);
    }
}

 

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

 

package cn.test;

import java.util.Arrays;

public class Work3 {

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

        }
        System.out.println("...............");
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (a[j] > a[j + 1]) {
                    int b = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = b;
                }

            }

        }
        for (int i : a) {
            System.out.println(i);
        }
    }

}

 

4、 输出一个double型二维数组(长度分别为54,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)

 

package cn.test;

public class Work4 {

    /**
     * @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] = i+j;
            }
        }
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.println(a[i][j] + " ");
            }
            System.out.println();
        }

    }
}

 

5.在一个有8个整数(18257361328963)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?

 

package cn.test;

public class Work5 {

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

 

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

package cn.test;



public class Work6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int num[]={2021,4,23};
        for (int i = 0; i < num.length/2; i++) {
            int temp;
            temp=num[i];
            num[i]=num[num.length-1-i];
            num[num.length-1-i]=temp;
        }
        for (int i = 0; i < num.length; i++) {
            System.out.println(num[i]);
        }
     }
}

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

 
package cn.test;

public class Work7{

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[] = new int[] { 3, 4, 4, 5, 7, 7, 9 };
        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 m : a) {
            System.out.print(m);
        }
    }

}

 

 

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

package cn.test;

public class Work8 {

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

}

9、使用数组存放裴波那契数列的前20项 ,并输出

package cn.test;

public class Work9 {

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

}

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

 

 

import java.util.Arrays;
  import java.util.Random;
  
  public class Work10 {

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

    }
 
 }

 

 

posted @ 2021-04-24 20:02  ·梓清  阅读(47)  评论(0编辑  收藏  举报