在控制台打印一个菱形&写一段程序,取两个数组的交集

1.在控制台打印一个菱形。
 1 package lession0711B;
 2 
 3 public class TestB1 {
 4 
 5     public static void main(String[] args) {
 6         int n = 7;
 7         for (int i = 1; i <= n; i++) {// 控制输出几行
 8 
 9             // 1.空格
10             for (int j = 1; j <= (i - n / 2 - 1 > 0 ? i - n / 2 - 1 : n / 2 + 1 - i); j++) {
11                 System.out.print(" ");
12             }
13 
14             // 2.星号
15             for (int k = 1; k <= n - 2 * (i - n / 2 - 1 > 0 ? i - n / 2 - 1 : n / 2 + 1 - i); k++) {
16                 System.out.print("*");
17             }
18             System.out.println();
19         }
20     }
21 
22 }

 

 

 

 

 

 

2.写一段程序,取两个数组的交集。
 
 1 package lession0711B;
 2 
 3 import java.util.Arrays;
 4 
 5 public class Test2 {
 6 
 7     public static void main(String[] args) {
 8         int[] array1 = new int[5];
 9         int[] array2 = new int[5];
10         int[] array3 = new int[5];
11         for (int i = 0; i < 5; i++) {// 给数组A随机赋值5个
12             array1[i] = (int) (Math.random() * 10);
13         }
14         for (int i = 0; i < 5; i++) {// 给数组B随机赋值5个
15             array2[i] = (int) (Math.random() * 10);
16         }
17         int cont = 0;
18         for (int i = 0; i < 5; i++) {// 把A、B数组中相同的值赋值给数组C
19             for (int j = 0; j < 5; j++) {
20                 if (array1[i] == array2[j]) {
21                     array3[cont] = array1[i];
22                     cont++;
23                     break;
24                 }
25             }
26         }
27         int a = 1;
28         if (array3[4] == 0) {
29             for (int i = 0; i < 4; i++) {// 判断数组A中是否有0
30                 if (array1[i] == 0) {
31                     break;
32                 } else if (i == 3) {
33                     a -= 1;
34                 }
35             }
36             for (int i = 0; i < 4; i++) {// 判断数组B中是否有0
37                 if (array2[i] == 0) {
38                     break;
39                 } else if (i == 3 && a == 1) {
40                     a -= 1;
41                 }
42             }
43         }
44         for (int i = 0; i < 4; i++) {// 判断A、B数组中相同值的个数
45             for (int j = i + 1; j < 4; j++)
46                 if (array3[i] == array3[j]) {
47                     break;
48                 } else if (j == 3) {
49                     a++;
50                 }
51         }
52         int[] array4 = new int[a];
53         int m = 0;
54         for (int i = 0; i < 4; i++) {// 把array3数组中的值赋给数组array4
55             for (int j = i + 1; j < 4; j++)
56                 if (array3[i] == array3[j]) {
57                     break;
58                 } else if (j == 3) {
59                     array4[m] = array3[i];
60                     m++;
61                 }
62         }
63 //        System.out.println(a);
64         System.out.println("数组A:" + Arrays.toString(array1));
65         System.out.println("数组B:" + Arrays.toString(array2));
66 //        System.out.println("数组A和B的交集:"+Arrays.toString(array3));
67         if (a != 0) {
68             System.out.println("数组A和B的交集:" + Arrays.toString(array4));
69         } else {
70             System.out.println("两个数组没有交集!");
71         }
72     }
73 
74 }
View Code

 

 
集合要去重并要数组中多余的0:

 

 

 

 

 

 

 

 

 

posted @ 2022-07-11 22:29  扫码登录  阅读(27)  评论(0)    收藏  举报