1.有10个评委打分,(去掉一个最高一个最低)求平均分。 package ssa; import java.util.Scanner; public class lianxi { public static void main(String[] args) { int[] arry = new int[] { 1,2,3,4,5,6,7,8,9,10}; for (int i = 0; i < arry.length - 1; i++) { for (int j = 0; j <arry.length - 1 - i; j++) { if (arry[j] > arry[j + 1]) { int tmp = arry[j]; arry[j] = arry[j + 1]; arry[j + 1] = tmp; } } } int sum = 0; for (int i = 1; i < arry.length - 1; i++) { sum += arry[i]; } System.out.println("平均分为:" + sum / (arry.length - 2.0)); } }
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。 package ssa; import java.util.Random; public class lianxi { public static void main(String[] args) { int[] arry = new int[10]; Random r=new Random(); for (int i = 0; i < arry.length; i++) { arry[i]=r.nextInt(100); } int t; for (int i = 0; i < arry.length - 1; i++) { for (int j = 0; j < arry.length-1-i; j++) { if (arry[j + 1] >arry[j]) { t = arry[j]; arry[j] = arry[j + 1]; arry[j + 1] = t; } } } for (int i=0;i<arry.length;i++) { System.out.println(i); } } }
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数) package ssa; import java.util.Random; public class lianxi { public static void main(String[] args) { int a[] = new int[7]; Random r = new Random(); for (int i = 0; i < a.length; i++) { a[i] = r.nextInt(35)+1; } for (int x = 0; x < a.length; x++) { System.out.println(a[x]); } } }
4.定义一个长度为10的int数组(如果没有特殊说明,静态赋值动态赋值都可以),统计数组中的最大值、最小值、以及奇 数和偶数的个数。 package ssa; import java.util.Random; public class lianxi { public static void main(String[] args) { int a[] = { 10, 20, 33, 34, 87, 90, 88, 9, 38, 44 }, q = 0; int o = 0, max = a[0], min = a[0]; for (int i = 0; i < a.length; i++) { if (a[i] % 2 == 0) { o = o + 1; } else { q = q + 1; } } for (int x = 0; x < a.length; x++) { if (a[x] > max) { max = a[x]; } } for (int y = 0; y < a.length; y++) { if (a[y] < min) { min = a[y]; } } System.out.println("最大值是" + max + ",最小值是" + min + ",奇数有" + q + "个,偶数有" + o + "个"); }