第七周上机练习
1.有10个评委打分,(去掉一个最高一个最低)求平均分
package Svd; public class bbt { public static void main(String[] args) { // TODO Auto-generated method stub int arr[] = { 20, 40, 30, 10, 50, 60, 35, 25, 45, 15 }; double a = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { int b = 0; if (arr[i] > arr[j]) { b = arr[i]; arr[i] = arr[j]; arr[j] = b; break; } } } arr[0] = 0; arr[9] = 0; for (int x = 0; x < 10; x++) { a = arr[x] + a; } a = a / 8; System.out.println("平均分是:" + a); } }
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出
package Svd; import java.util.Random; public class bbt { public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = new int[10]; Random a = new Random(); for (int i = 0; i < 10; i++) { arr[i] = a.nextInt(100); } for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int b = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = b; } } } System.out.println("排序后输出的是:"); for (int i = 0; i < 10; i++) { System.out.println(arr[i]); } } }
3.制作彩票35选7程序。(就是1~35随机生成7个不重复的数)
package Svd; import java.util.Random; public class bbt { public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = new int[7]; Random r = new Random(); for (int i = 0; i < arr.length; i++) { arr[i] = r.nextInt(35) + 1; } for (int i = 0; i < arr.length; i++) { System.out.print("35选7号码为:"); System.out.println(arr[i]); } } }
4.定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数
package Svd; import java.util.*; public class bbt { public static void main(String[] args) { // TODO Auto-generated method stub int max = 10; int min = 1; int a = 0; int b = 0; int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int i = 0; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } if (arr[i] < min) { min = arr[i]; } } for (int i = 0; i < arr.length; i++) { if (arr[i] % 2 == 0) { a++; } else { b++; } } System.out.println("最大值:" + max); System.out.println("最小值:" + min); System.out.println("偶数出现的次数:" + a); System.out.println("奇数出现的次数:" + b); } }