2020.4.16.java上机作业
1.有10个评委打分,(去掉一个最高一个最低)求平均分
package com.itheima01; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { avg10(); } public static void avg10() { int[] array = {3, 5, 1, 0, 10, 9, 8, 9, 4, 6}; 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.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出
package com.itheima01; import java.util.Random; public class HelloWorld { public static void main(String[] args) { int[] a = new int[10]; Random r=new Random(); for (int i = 0; i < a.length; i++) { a[i]=r.nextInt(100); } int t; for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length-1-i; j++) { if (a[j + 1] > a[j]) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } for (int i : a) { System.out.println(i); } } }
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)
package com.itheima01; import java.util.Random; public class HelloWorld { 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 com.itheima01; import java.util.Random; public class HelloWorld { public static void main(String[] args) { Random r=new Random (); int []arr=new int [10]; for (int i = 0; i < arr.length; i++) { arr[i]=r.nextInt(100); } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } int max=0,min=arr[0],jishu=0,oushu=0; for (int i = 0; i < arr.length; i++) { if(max<arr[i]) max=arr[i]; } for (int i = 0; i < arr.length; i++) { if(arr[i]<min) min=arr[i]; } for (int i = 0; i < arr.length; i++) { if(arr[i]%2==0) oushu+=1; else jishu+=1; } System.out.println("max="+max+",min="+min+",奇数个数"+jishu+",偶数个数"+oushu); } }