4.16
1.有10个评委打分,(去掉一个最高一个最低)求平均分。
package text1; public class school5 { public static void main(String[] args) { // TODO Auto-generated method stub double []a=new double[] {9.1,10,8.2,9.5,6.7,6.6,4.2,5.5,6.6,7.7}; double max=0.0; double min=0.0; double sum=0.0; double everage=0.0; for(int i=0;i<a.length-1;i++) { if(a[i+1]>a[i]) { max=a[i]; a[i]=a[i+1]; a[i+1]=max; } } for(int j=0;j<a.length-1;j++) { if(a[j+1]<a[j]) { min=a[j]; a[j]=a[j+1]; a[j+1]=min; } sum=sum+a[j]; } everage=(sum-min-max)/(a.length-2); System.out.println("最高得分为:"+max+"最低得分为:"+min+"平均分为:"+everage); } }
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。
package text1; import java.util.Random; public class school5 { 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); } 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 text1; import java.util.Random; public class school5 { public static void main(String[] args) { // TODO Auto-generated method stub int[] a= new int[35]; Random r=new Random(); for (int i = 0; i < a.length; i++) { a[i]=r.nextInt(34)+1; } int[] b=new int[7]; for (int j = 0; j < 7; j++) { b[j]=a[r.nextInt(36)]; } for (int i : b) { System.out.print(i+" "); } } }
4.定义一个长度为10的int数组(如果没有特殊说明,静态赋值动态赋值都可以),统计数组中的最大值、最小值、以及奇 数和偶数的个数
package text1; public class school5 { public static void main(String[] args) { // TODO Auto-generated method stub int[] a = new int[]{44,33,22,11,55,66,77,88,99,67}; int max=a[0]; int min=a[0]; int js=0; int os=0; for (int i = 1; i <a.length ; i++) { if (a[i]>max){ max=a[i]; } } for (int j = 1; j <a.length ; j++) { if (a[j]<min){ min=a[j]; } } for (int k = 0; k< a.length; k++) { if (a[k]%2==0){ os+=1; }else { js+=1; } } System.out.println("最大值为"+max+"最小值为"+min+"偶数有"+os+"个"+"奇数有"+js+"个"); } }