1.有10个评委打分,(去掉一个最高一个最低)求平均分。
package lucky; public class happy { public static void main(String[] args) { // TODO Auto-generated method stub int a[] = {2,4,6,8,10,12,14,16,18,20}; int m = 0; for(int i=0;i<10;i++){ for(int j=0;j<10;j++){ int n = 0 ; if(a[i]>a[j]){ n=a[i]; a[i]=a[j]; a[j]=n; } } } a[0]=0; a[9]=0; for(int x=0;x<10;x++){ m=a[x]+m; } m=m/8; System.out.println("平均分是:"+m); } }
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。
package lucky; import java.util.Random; public class happy { 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 lucky; import java.util.Random; public class happy { public static void main(String[] args) { // TODO Auto-generated method stub int a[] = new int[7]; Random r = new Random(); for(int i=0;i<a.length;i++){ a[i] = r.nextInt(35)+1; }for(int j=0;j<a.length;j++){ System.out.println(a[j]); } } }
4.定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数
package lucky; public class happy { public static void main(String[] args) { // TODO Auto-generated method stub int[] num = new int[]{2,4,6,8,11,12,15,16,19,20}; int numMax = num[0]; int numMin = num[0]; int numjs = 0; int numos = 0; for(int i=0;i<num.length;i++){ if(num[i]>numMax){ numMax=num[i]; }if(num[i]<numMin){ numMin=num[i]; }if(num[i]%2==0){ numos +=1; }else{ numjs +=1; } } System.out.println("数组的最大值为:"+numMax); System.out.println("数组的最小值为:"+numMin); System.out.println("数组的偶数个数为:"+numos); System.out.println("数组的奇数个数为:"+numjs); } }