4.16第七周上机作业
1.有10个评委打分,(去掉一个最高一个最低)求平均分。
import java.util.*; public class Lei { public static void main(String[] args) { int a[] = {5,4,8,6,7,6,5,8,8,10}; int q = 0; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { int temp = 0; if (a[i]>a[j]) { temp=a[i]; a[j]=temp; break; } } } a[0]=0; a[9]=0; for (int x = 0; x < a.length; x++) { q=a[x]+q; } q=q/8; System.out.println("平均分是:" + q); } }
2.Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。
{45,88,72,32,}
package bao; import java.util.*; public class Lei { 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 bao; import java.util.Random; import java.util.*; public class Lei { 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 bao; import java.io.IOException; import java.util.*; public class Lei { public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] a = new int[10]; System.out.println("请输入10个数:"); for (int i = 0; i < 10; i++) { a[i] = input.nextInt(); } int max = a[0], min = a[0], m = 0, n = 0; for (int i = 0; i < 10; i++) { if (max < a[i]) { max = a[i]; } if (min > a[i]) { min = a[i]; } if (a[i] % 2 == 0) { m++; } if (a[i] % 2 != 0) { n++; } } System.out.println("最大值是:" + max + " 最小值是:" + min + " 奇数的个数是:" + n + " 偶数的个数是:" + m); } }