2020.10.14日作业
第一题
package com.hp.www; public class day3 { public static void main(String[] args) { int[] arr={2,3,4,5,6,7,8,9,10,11}; //遍历打印 // for (int i = 0; i < arr.length; i++) { // System.out.println(arr[i]);} //打印最小值 int max = 0; int min = 0; max = min = arr[0]; for (int x = 0; x < arr.length; x++) { if (arr[x] > max) { max = arr[x]; } if(arr[x]<min){ min = arr[x]; } } System.out.println("最大数是:" + max); System.out.println("最小数是:" + min); //求数组第二大值 int shu1=arr[0]; int shu2=arr[0]; for (int i = 0; i < args.length; i++) { if (arr[i] >shu1) { shu2=shu1; shu1=arr[i]; }else if(arr[i]>shu2){ shu2=arr[i]; } } System.out.println("第二个数是:"+shu2); } }
第二题
package com.hp.www; public class day32 { public static void main(String[] args) { int[] arr = {8,3,6,9,14,99,55,44,33,21}; int a=0; int mean = 0; for (int i = 0; i < arr.length; i++) { mean += arr[i]; } a=mean/arr.length; System.out.println("平均值" + a); int sum1 = 0; for (int i = 0; i < arr.length; i++) { if(arr[i]>a){ sum1=arr[i]; } } System.out.println(sum1); } }
第三题
1 package com.hp.www; 2 3 public class day33 { 4 public static void main(String[] args) { 5 int [] arr={1,2,3,4,11}; 6 for (int i = 0; i <arr.length ; i++) { 7 if (arr[i]<10) { 8 System.out.println(arr[i]); 9 }else { 10 System.out.println("未找到合适的数据"); 11 } 12 } 13 } 14 }
第四题
1 package com.hp.www; 2 public class day34 { 3 public static void main(String[] args) { 4 int[] arr = {8,3,6,9,14,99,55,44,33,21}; 5 int a=0; 6 int mean = 0; 7 for (int i = 0; i < arr.length; i++) { 8 mean += arr[i]; 9 } 10 a=mean/arr.length; 11 System.out.println("平均值" + a); 12 int b=0; 13 int sum1 = 0; 14 for (int i = 0; i < arr.length; i++) { 15 if(arr[i]>a){ 16 sum1++; 17 b+=arr[i]; 18 } 19 } 20 System.out.println("大于平均数的个数有"+sum1+"个"+"和为"+b); 21 } 22 }
第五题
1 package com.hp.www; 2 3 public class day35 { 4 public static void main(String[] args) { 5 int[] arr1 = {1,2,3}; 6 int[] arr2 = {4,5,6}; 7 int[] arr3=merge(arr1,arr2); 8 for (int i = 0; i < arr3.length; i++) { 9 System.out.println(arr3[i]); 10 } 11 } 12 public static int[] merge(int[]arr1,int[] arr2){ 13 int[] a=new int[arr1.length+arr2.length]; 14 for (int i = 0; i <arr1.length ; i++) { 15 a[i]=arr1[i]; 16 } 17 for (int i = 0; i <arr2.length ; i++) { 18 a[arr1.length+i]=arr2[i]; 19 } 20 return a; 21 }}
第六题
package com.hp.www; public class day36 { public static void main(String[] args) { int[] arr={171,72,19,16,118,51,210,7,18}; int a=0; for (int i = 0; i <arr.length ; i++) { if(arr[i]%10!=7&&arr[i]/10%100!=7&&arr[i]%2==0){ System.out.println(arr[i]); a=arr[i]; } System.out.println("满足要求的元素和是:"+a); } } }