4.16
作者:互联网
1.有10个评委打分,(去掉一个最高一个最低)求平均分。
package Text; public class zuoye9 { public static void main(String[] args) { int a[] = {1,2,2,4,5,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 Text; import java.util.Random; public class zuoye9 { 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 Text; import java.io.IOException; import java.util.Scanner; public class zuoye9 { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); int[] r = new int[7]; boolean[] b = new boolean[31]; for (int i = 0; i < 7; i++) { int tem = (int) (Math.random() * 30) + 1; while (b[tem] == true) tem = (int) (Math.random() * 30) + 1; r[i] = tem; b[tem] = true; } int[] in = new int[7]; for (int i = 0; i < 7; i++) { in[i] = scanner.nextInt(); } System.out.println("Random numbers:"); for (int i = 0; i < 7; i++) { System.out.print(r[i] + " "); } System.out.println(); System.out.println("Your numbers:"); for (int i = 0; i < 7; i++) { System.out.print(in[i] + " "); } System.out.println(); System.out.println("The same numbers:"); for (int i = 0; i < 7; i++) for (int j = 0; j < 7; j++) { if (in[i] == r[j]) { System.out.print(in[i] + " "); } } System.out.println(); } }
4.定义一个长度为10的int数组(如果没有特殊说明,静态赋值动态赋值都可以),统计数组中的最大值、最小值、以及奇 数和偶数的个数
package Text; import java.util.Scanner; public class zuoye9 { 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); } }
标签:10,4.16,int,System,++,println,out 来源: https://www.cnblogs.com/cuixiaorongshazi/p/12711762.html