标签:彩票 范围 ++ length java随机数 pre inf method ==
1.有10个评委打分,(去掉一个最高一个最低)求平均分。
1 package as; 2 3 public class unll { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 7 int a[]={80,60,20,40,90,50,80,70,90,20}; 8 double q=0; 9 for(int i=0;i<10;i++){ 10 for(int j=0;j<10;j++){ 11 int temp=0; 12 if(a[i]>a[j]){ 13 temp=a[i]; 14 a[i]=a[j]; 15 a[j]=temp; 16 } 17 } 18 } 19 20 a[0]=0; 21 a[9]=0; 22 for(int x=0;x<10;x++){ 23 q=a[x]+q; 24 } 25 q=q/8; 26 System.out.println("平均分是"+q); 27 } 28 }
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。
1 package as; 2 import java.util.Random; 3 public class unll { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 int[] arr = new int[10]; 7 Random ran = new Random(); 8 System.out.println("产生的随机数组为:"); 9 for (int i = 0; i < arr.length; i++) { 10 arr[i] = ran.nextInt(100); 11 System.out.print(arr[i] + " "); 12 } 13 System.out.println(); 14 for (int i = 0; i < arr.length-1; i++) { 15 for (int j = 0; j < arr.length-1-i; j++) { 16 if (arr[j]>arr[j+1]) { 17 int temp = arr[j]; 18 arr[j] = arr[j+1]; 19 arr[j+1] = temp; 20 } 21 } 22 } 23 System.out.println("排序后输出结果为:"); 24 for (int i = 0; i < arr.length; i++) { 25 System.out.print(arr[i] + " "); 26 } 27 } 28 }
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)
1 package as; 2 import java.util.Random; 3 public class unll { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 int[] a = new int[7]; 7 Random b = new Random(); 8 for (int i = 0; i < 7; i++) { 9 a[i] = b.nextInt(35) + 1; 10 for (int j = 0; j < i; j++) { 11 if (a[j] == a[i]) { 12 i--; 13 } 14 } 15 } 16 for (int i = 0; i < a.length; i++) { 17 System.out.println("35选7的号码为:" + a[i]); 18 } 19 } 20 }
4.定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数
1 package as; 2 3 public class unll { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 int a[] = {10,25,1,4,5,6,32,8,9,7}; 7 int max = a[0]; 8 int min = a[0]; 9 int countQ = 0; 10 int countO = 0; 11 for (int i = 0; i < a.length; i++) { 12 if (a[i]>max) { 13 max = a[i]; 14 } 15 if (a[i]<min) { 16 min = a[i]; 17 } 18 if (a[i]%2==0) { 19 countQ += 1; 20 }else { 21 countO += 1; 22 } 23 } 24 System.out.println("数组的最大值为:" + max); 25 System.out.println("数组的最小值为:" + min); 26 System.out.println("数组的偶数个数为:" + countO); 27 System.out.println("数组的奇数个数为:" + countQ); 28 } 29 }
标签:彩票 范围 ++ length java随机数 pre inf method ==
原文地址:https://www.cnblogs.com/zxp-0101/p/12711791.html