第七周上机作业
//1.有10个评委打分,(去掉一个最高一个最低)求平均分。 package com.a01; import java.util.*; public class hellowold { public static void main(String[] args) { int t = 0, sum = 0; int[] a = new int[10]; Scanner input = new Scanner(System.in); for (int i = 0; i < a.length; i++) { System.out.print("请输入第" + (i + 1) + "个数:"); a[i] = input.nextInt(); } System.out.println("10个评委给出的分数为:"); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9 - i; j++) { if (a[j + 1] > a[j]) { t = a[j + 1]; a[j + 1] = a[j]; a[j] = t; } } } for (int i = 1; i < 9; i++) { sum += a[i]; } System.out.println("去掉一个最高分" + a[0] + ",最低分" + a[9] + ",平均分为:" + sum / 8.0); } }
//2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。 package com.a01; import java.util.Random; public class hellowold { public static void main(String[] args) { int[] n = new int[10]; Random r = new Random(); for (int i = 0; i < 10; i++) { int a = r.nextInt(100); n[i] = a; System.out.println("第" + (i + 1) + "个随机数" + n[i]); } int t = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9 - i; j++) { if (n[j + 1] > n[j]) { t = n[j + 1]; n[j + 1] = n[j]; n[j] = t; } } } for (int i = 0; i < n.length; i++) { System.out.println("排序输出后:" + n[i]); } } }
//3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数) package com.a01; import java.util.Random; public class hellowold { public static void main(String[] args) { Random r = new Random(); for (int i = 0; i < 7; i++) { int a = r.nextInt(5) + (i * 5); System.out.println("第" + (i + 1) + "数:" + a); } } }
// 4.定义一个长度为10的int数组(如果没有特殊说明,静态赋值动态赋值都可以),统计数组中的最大值、最小值、以及奇 数和偶数的个数 package com.a01; import java.util.Random; public class hellowold { public static void main(String[] args) { Random r = new Random(); int[] n = new int[10]; for (int i = 0; i < 10; i++) { int a = r.nextInt(20); n[i] = a; System.out.println("第" + (i + 1) + "数:" + n[i]); } int t = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9 - i; j++) { if (n[j + 1] > n[j]) { t = n[j + 1]; n[j + 1] = n[j]; n[j] = t; } } } int mm = 0, qq = 0; for (int i = 0; i < 10; i++) { if (n[i] % 2 == 0) { mm++; } else { qq++; } } System.out.println("数组中最大值为:" + n[0]); System.out.println("最小值为:" + n[9]); System.out.println("奇数,偶数分别出现的次数为:" + qq + "," + mm); } }