1.有10个评委打分,(去掉一个最高一个最低)求平均分。
package calsswork; import java.util.Scanner; public class text1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a[] = new int[10]; double sum = 0; int b = 1; double c = 0; for (int i = 0; i < a.length; i++) { System.out.println("输入数组" ); a[i] = sc.nextInt(); } for (int x = 0; x < a.length - 1; x++) { for (int y = 0; y < a.length - 1 - x; y++) { if (a[y] > a[y + 1]) { int temp = a[y + 1]; a[y + 1] = a[y]; a[y] = temp; } } } for (int n : a) { System.out.println(n + "、"); } for (; b < a.length - 1; b++) { sum += a[b]; c = sum / b; } System.out.println("平均数:" + c); } }
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。
package calsswork; import java.util.Scanner; public class text1 { public static void main(String[] args) { int arr[] = new int[10]; Random r = new Random(); for (int i = 0; i < arr.length; i++) { arr[i] = r.nextInt(100); System.out.print(arr[i] + ", "); } System.out.println(); System.out.println("随机数开始排序!!!"); for (int x = 0; x < arr.length - 1; x++) { for (int y = 0; y < arr.length - 1 - x; y++) { if (arr[y] > arr[y + 1]) { int temp = arr[y + 1]; arr[y + 1] = arr[y]; arr[y] = temp; } } } for (int n : arr) { System.out.println(n + ", "); } } }
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)
package calsswork; import java.util.Random; public class text1 { public static void main(String[] args) { 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 calsswork; import java.util.Scanner; public class text1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int arr[] = new int[10]; int js = 0, os = 0; for (int i = 0; i < arr.length; i++) { System.out.println("请输入第" + (i + 1) + "个数:"); arr[i] = sc.nextInt(); } int max = arr[0], min = arr[0]; for (int x = 1; x < arr.length; x++) { if (arr[x] > max) { max = arr[x]; } else if (arr[x] < min) { min = arr[x]; } } for (int y = 0; y < arr.length; y++) { if (arr[y] % 2 == 0) { os++; } else { js++; } } System.out.println("最大值为:" + max + "," + "最小值为:" + min); System.out.println("奇数个数为:" + js + "," + "偶数个数为:" + os); } }