第七周上机作业
1.有10个评委打分,(去掉一个最高一个最低)求平均分。
package the; import java.util.Scanner; public class newclass { 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 x = 0; x < a.length; x++) { if (a[x] < min) { min = a[x]; } } for (int y = 0; y < a.length; y++) { sum += a[y]; } System.out.println("平均分是" + (sum -max-min)/ 8.0); } }
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。
package the; import java.util.Random; public class newclass { public static void main(String[] args) { // TODO Auto-generated method stub int a[] = new int[10]; Random r = new Random(); for (int i = 0; i < a.length; i++) { a[i] = r.nextInt(100); } for (int i = 0; i < a.length - 1; i++) { for (int n = 0; n < a.length - 1 - i; n++) { if (a[n] > a[n + 1]) { int c = a[n]; a[n] = a[n + 1]; a[n + 1] = c; } } } for (int x = 0; x < a.length; x++) { System.out.print(a[x] + " "); } }
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)
package the; import java.util.Random; public class newclass { public static void main(String[] args) { // TODO Auto-generated method stub 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 the; import java.util.Scanner; public class newclass { public static void main(String[] args) { // TODO Auto-generated method stub int max = 0, min = 99; int os = 0, js = 0; int[] a = { 22, 65, 23, 82, 31, 1, 9, 87, 99, 70 }; 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("最大值:" + max); System.out.println("最小值:" + min); System.out.println("偶数个数:" + os); System.out.println("奇数个数:" + js); } }