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