4.16
1.有10个评委打分,(去掉一个最高一个最低)求平均分。
public class first { public static void main(String[] args) { int[] a = new int[]{1, 3, 2, 5, 8}; int t; for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (a[j + 1] > a[j]) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } double sum=0; for (int k = 1; k <a.length-1 ; k++) { sum+=a[k]; } System.out.println("平均分"+sum/(a.length-2)); } }
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。
import java.util.Random; public class first { public static void main(String[] args) { int[] a = new int[10]; Random r=new Random(); for (int i = 0; i < a.length; i++) { a[i]=r.nextInt(100); } int t; for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length-1-i; j++) { if (a[j + 1] > a[j]) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } for (int i : a) { System.out.println(i); } } }
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)
import java.util.Random; public class first { public static void main(String[] args) { int[] a = new int[35]; Random r=new Random(); for (int i = 0; i < a.length; i++) { a[i]=r.nextInt(35)+1; } int[] b=new int[7]; for (int j = 0; j < 7; j++) { b[j]=a[r.nextInt(36)]; } for (int i : b) { System.out.print(i+","); } } }
4.定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数
public class first { public static void main(String[] args) { int[] a = new int[]{2,3,52,3,69,58,46,12,53,27}; int max=a[0]; int mini=a[0]; int jishu=0; int oushu=0; for (int i = 1; i <a.length ; i++) { if (a[i]>max){ max=a[i]; } } for (int j = 1; j <a.length ; j++) { if (a[j]<mini){ mini=a[j]; } } for (int k = 0; k< a.length; k++) { if (a[k]%2==0){ oushu+=1; }else { jishu+=1; } } System.out.println("最大值为"+max+"最小值为"+mini+"偶数有"+oushu+"个"+"奇数有"+jishu+"个"); } }