1.有10个评委打分,(去掉一个最高一个最低)求平均分。
package learn; import java.util.*; public class learn1{ public static void main(String[] args){ Scanner s = new Scanner(System.in); double[] arry = new double[10]; System.out.println("请输入10个评委的打分"); for (int i = 0; i < 10; i++) { arry[i] = s.nextDouble();//分数 } double temp = 0; for (int i = 0; i < arry.length; i++) { for (int j = 0; j < arry.length-i-1; j++) { if (arry[j]<arry[j+1]) { temp = arry[j]; arry[j] = arry[j+1]; arry [j+1] = temp; } } } arry[0]=0; arry[9]=0; double sum=0,pjs; for (int i = 0; i < arry.length; i++) { sum+=arry[i]; } pjs=sum/8; System.out.println("他的平均分是"+pjs); } }
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。
package learn; import java.util.*; public class learn1{ public static void main(String[] args){ int ARRAYLENGTH = 10; int a[] = new int[ARRAYLENGTH]; Random ran = new Random(); for(int i = 0; i < a.length; i++){ //Random类nextInt()方法产生随机数 a[i] = ran.nextInt(99) + 1; } System.out.println("数组a为:" + Arrays.toString(a)); } }
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)
package learn; import java.util.*; public class learn1{ public static void main(String[] args){ List<Integer> reList = new ArrayList<Integer>(); int[] num = new int[35];// 创建一个35长度的int数组 boolean[] flag = new boolean[35]; int count = 0; for (int i = 0; i < 35; i++) { num[i] = i + 1; flag[i] = true; } // 判断抓取到的数字够不够7个,不够的话,继续抓取 while (count != 7) { int k = (int) (Math.random() * 35); if (flag[k]) { reList.add(num[k]); flag[k] = false; count++; } } // 抓取结束后输出抓取结果 Object[] result = reList.toArray(); System.out.print("[\t"); for (int i = 0; i < result.length; i++) { System.out.print(result[i] + "\t"); } System.out.print("]"); } }
4.定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数
package learn; public class learn1{ public static void main(String[] args){ int[] num=new int[] {99,57,77,45,89,43,66,85,86,55}; int max=num[0]; int mini=num[0]; int jishu=0; int oushu=0; for (int i = 1; i <num.length ; i++) { if (num[i]>max){ max=num[i]; } } for (int j = 1; j <num.length ; j++) { if (num[j]<mini){ mini=num[j]; } } for (int k = 0; k< num.length; k++) { if (num[k]%2==0){ oushu+=1; }else { jishu+=1; } } System.out.println("最大值为"+max+ "最小值为"+mini+ "偶数有"+oushu+"个"+ "奇数有"+jishu+"个"); } }