1.有10个评委打分,(去掉一个最高一个最低)求平均分。
package zy; import java.util.Scanner;// public class hj { public static void main(String[] args) { Scanner input=new Scanner(System.in);// int[] array = {100,50,90,90,90,90,90,90,90,90}; int index = 0, sum = 0, temp = 0, avg = 0 ; for (int i = 0; i < array.length - 1; i++) { for (int j = i + 1; j < array.length; j++) { if (array[j] < array[i]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } while (array.length != index) { sum += array[index]; index++; } do { sum = sum - array[0] - array[array.length-1]; } while (false); avg = sum / 8; System.out.println(avg); } }
2.生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。
package zy; import java.util.Scanner; import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class hj { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("请输入随机数范围"); System.out.println("请输入范围中最大值"); int t=input.nextInt(); System.out.println("请输入范围中最小值"); int t1=input.nextInt(); int a[]=new int [4]; Random r=new Random(); for(int y=0;y<a.length;y++) { a[y]=t1+r.nextInt(t); } Arrays.sort(a); for(int u:a) { System.out.print(u+"\t"); } } }
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)
package zy; import java.util.Random; public class hj { public static void main(String[] args) { int[] score = new int[7]; Random r = new Random(); for (int i = 0; i < score.length; i++) { score[i] = r.nextInt(35) + 1; } System.out.println("35选7号码为:"); for (int i = 0; i < score.length; i++) { System.out.println(score[i]); } } }
4.定义一个长度为10的int数组(如果没有特殊说明,静态赋值动态赋值都可以),统计数组中的最大值、最小值、以及奇 数和偶数的个数
package zy; public class hj { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int max = arr[0]; int min = arr[1]; for (int i = 0; i < arr.length; i++) { if (max < arr[i]) { max = arr[i]; } else if (min > arr[i]) { min = arr[i]; } } System.out.println("数组的最大值是" + max + "数组的最小值是" + min); int count1 = 0; int count2 = 0; for (int j = 0; j < arr.length; j++) { if (j % 2 != 0) { count1++; } else { count2++; } } System.out.println("奇数有" + count1 + "个," + "偶数有" + count2 + "个"); } }