4.16 上机作业
1.有10个评委打分,(去掉一个最高一个最低)求平均分。
package test1; import java.util.*; public class finish { public static void main(String[] args) { // TODO Auto-generated method stub int a[] = new int[10]; int sum = 0, max = 0, min = 1000; System.out.println("请输入分数"); for (int i = 0; i < a.length; i++) { Scanner input = new Scanner(System.in); a[i] = input.nextInt(); } for (int j = 0; j < a.length; j++) { if (a[j] > max) { max = a[j]; } } for (int m = 0; m < a.length; m++) { if (a[m] < min) { min = a[m]; } } for (int n = 0; n < a.length; n++) { sum += a[n]; } System.out.println("平均分是" + (sum -max-min)/ 8.0); } }
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出
package test1; import java.util.Scanner; import java.util.Random; public class finish { public static void main(String[] args) { // TODO Auto-generated method stub int arr[] = new int[10]; Random r = new Random(); for (int i = 0; i < arr.length; i++) { arr[i] = r.nextInt(100); } for (int i = 0; i < arr.length - 1; i++) { for (int n = 0; n < arr.length - 1 - i; n++) { if (arr[n] > arr[n + 1]) { int c = arr[n]; arr[n] = arr[n + 1]; arr[n + 1] = c; } } } for (int x = 0; x < arr.length; x++) { System.out.print(arr[x] + " "); } } }
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)
package test1; import java.util.Random; public class finish { public static void main(String[] args) { // TODO Auto-generated method stub int[] a = new int[7]; Random b = new Random(); for (int i = 0; i < 7; i++) { a[i] = b.nextInt(35) + 1; for (int j = 0; j < i; j++) { if (a[j] == a[i]) { i--; } } } System.out.println("35选7的号码为:"); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
4.定义一个长度为10的int数组(如果没有特殊说明,静态赋值动态赋值都可以),统计数组中的最大值、最小值、以及奇 数和偶数的个数
package test1; public class finish { public static void main(String[] args) { // TODO Auto-generated method stub int max=0,min=99; int os=0,js=0; int []a={94,2,35,67,19,98,42,37,11,72}; for (int i = 0; i < a.length; i++) { if (a[i]< min) { min = a[i]; } if (a[i] > max) { max = a[i]; } } for (int i = 0; i < a.length; i++) { if (a[i]%2==0) { os++; }else{ js++; } } System.out.println("偶数出现的次数:" + os); System.out.println("奇数出现的次数:" + js); System.out.println("最大值:" + max); System.out.println("最小值:" + min); } }