Java第八次作业

上机练习

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

 1 package array;
 2 
 3 public class Array1 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int[] a = new int[5];
11         for (int i = 0; i < a.length; i++) {
12             a[i] = (i + 1) * 10;
13             System.out.println(a[i]);
14         }
15     }
16 }

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

 1 package array;
 2 
 3 import java.util.Arrays;
 4 
 5 public class Array2 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         String[] a = { "neusofteducation" };
13         String[] b = new String[a.length];
14         System.arraycopy(a, 0, b, 0, a.length);
15         System.out.println(Arrays.toString(b));
16     }
17 
18 }

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

 1 package array;
 2 
 3 import java.util.Arrays;
 4 
 5 public class Array3 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         int[] arr = { 1, 6, 2, 3, 9, 4, 5, 7, 8 };
13         Arrays.sort(arr);
14         for (int i : arr) {
15             System.out.print(i + " ");
16         }
17         System.out.println();
18         System.out.println("==================");
19         // 冒泡排序
20         for (int j = 0; j < arr.length - 1; j++) {
21             for (int k = 0; k < arr.length - 1 - j; k++) {
22                 if (arr[k] > arr[k + 1]) {
23                     int temp = arr[k];
24                     arr[k] = arr[k + 1];
25                     arr[k + 1] = temp;
26                 }
27             }
28         }
29         for (int j : arr) {
30             System.out.print(j + " ");
31         }
32     }
33 
34 }

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

 1 package array;
 2 
 3 public class Array4 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         double[][] a = new double[5][4];
11         for (int i = 0; i < a.length; i++) {
12             for (int j = 0; j < a[i].length; j++) {
13                 a[i][j] = i+j;
14             }
15         }
16         for (int i = 0; i < a.length; i++) {
17             for (int j = 0; j < a[i].length; j++) {
18                 System.out.print(a[i][j] + "   ");
19             }
20             System.out.println();
21         }
22     }
23 }

5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?

 1 package array;
 2 
 3 public class Array5 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int[] a = { 18, 25, 7, 36, 13, 2, 89, 63 };
11         int max = a[0];
12         int maxidx = 0;
13         for (int i = 1; i < a.length; i++) {
14             if (a[i] > max) {
15                 max = a[i];
16                 maxidx = i;
17             }
18         }
19         System.out.println("最大值是" + max + "它的下标是" + maxidx);
20     }
21 
22 }

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

 1 package array;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Array6 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner input = new Scanner(System.in);
13         int[] a = new int[5];
14         for (int i = a.length - 1; i >= 0; i--) {
15             a[i] = input.nextInt();
16         }
17         for (int i : a) {
18             System.out.println(i);
19         }
20     }
21 
22 }

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

 1 package array;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Array7 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner input = new Scanner(System.in);
13         int[] a = new int[8];
14         for (int i = 0; i < a.length; i++) {
15             a[i] = input.nextInt();
16             for (int j = 0; j < a.length; j++) {
17                 if (a[i] == a[j] && i != j) {
18                     a[j] = 0;
19                 }
20             }
21         }
22         for (int i : a) {
23             System.out.println(i);
24         }
25     }
26 
27 }

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

 1 package array;
 2 
 3 import java.util.Arrays;
 4 
 5 public class Array8 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         int[] arr = { -10, 2, 3, 246, -100, 0, 5 };
13         Arrays.sort(arr);
14         System.out.println("最大值" + arr[arr.length - 1]);
15         System.out.println("最小值" + arr[0]);
16         double sum = 0;
17         for (int i = 0; i < arr.length; i++) {
18             sum += arr[i];
19         }
20         System.out.println("数组的平均值是" + sum / arr.length);
21     }
22 
23 }

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

 1 package array;
 2 
 3 public class Array9 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method s
10         int[] y = new int[20];
11         y[0] = 1;
12         y[1] = 1;
13         for (int i = 2; i < y.length; i++) {
14             y[i] = y[i - 1] + y[i - 2];
15         }
16         for (int i : y) {
17             System.out.print(i+" ");
18         }
19     }
20 
21 }

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

 1 package array;
 2 
 3 import java.util.Arrays;
 4 import java.util.Random;
 5 
 6 public class Array10 {
 7 
 8     /**
 9      * @param args
10      */
11     public static void main(String[] args) {
12         // TODO Auto-generated method stub
13         Random r = new Random();
14         int[] p = new int[10];
15         for (int i = 0; i < p.length; i++) {
16             p[i] = r.nextInt(101);
17             System.out.print(p[i] + " ");
18         }
19         System.out.println();
20         Arrays.sort(p);
21         for (int i : p) {
22             System.out.print(i + " ");
23         }
24     }
25 
26 }

 

posted @ 2021-04-23 18:54  MXT16  阅读(52)  评论(0编辑  收藏  举报