第七周上机作业
1有10个评委打分,(去掉一个最高一个最低)求平均。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package day01; import java.util.*; public class test3 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input= new Scanner (System.in); int [] arr= new int [ 10 ]; int sum= 0 ; System.out.println( "请输入成绩" ); for ( int i = 0 ; i < arr.length; i++) { System.out.print(i+ 1 + ":" ); arr[i]=input.nextInt(); } for ( int i = 0 ; i < arr.length; i++) { for ( int j = 0 ; j < arr.length-i- 1 ; j++) { if (arr[j]>arr[j+ 1 ]){ int h=arr[j]; arr[j]=arr[j+ 1 ]; arr[j+ 1 ]=h; } } } for ( int i = 1 ; i < 9 ; i++) { sum+=arr[i]; } System.out.println( "选手分数平均值为" +sum/ 8.0 ); } } |
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package day01; import java.util.Random; public class yeah { public static void main(String[] args) { // TODO Auto-generated method stub Random r= new Random (); int []arr= new int [ 10 ]; for ( int i = 0 ; i < arr.length; i++) { arr[i]=r.nextInt( 100 ); } for ( int i = 0 ; i < arr.length; i++) { for ( int j = 0 ; j < arr.length-i- 1 ; j++) { if (arr[j]>arr[j+ 1 ]){ int h=arr[j]; arr[j]=arr[j+ 1 ]; arr[j+ 1 ]=h; } } } for ( int i = 0 ; i < arr.length; i++) { System.out.println(arr[i]); } } } |
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package day01; import java.util.Random; public class yeah { public static void main(String[] args) { // TODO Auto-generated method stub Random r= new Random (); int []arr= new int [ 7 ]; for ( int i = 0 ; i < arr.length; i++) { arr[i]=r.nextInt( 35 )+ 1 ; } for ( int i = 0 ; i < arr.length; i++) { System.out.print(arr[i]+ " " ); } } } |
4.定义一个长度为10的int数组(如果没有特殊说明,静态赋值动态赋值都可以),统计数组中的最大值、最小值、以及奇 数和偶数的个数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package day01; import java.util.Random; public class yeah { public static void main(String[] args) { // TODO Auto-generated method stub Random r= new Random (); int []arr= new int [ 10 ]; for ( int i = 0 ; i < arr.length; i++) { arr[i]=r.nextInt( 100 ); } for ( int i = 0 ; i < arr.length; i++) { System.out.print(arr[i]+ " " ); } int max= 0 ,min=arr[ 0 ],jishu= 0 ,oushu= 0 ; for ( int i = 0 ; i < arr.length; i++) { if (max<arr[i]) max=arr[i]; } for ( int i = 0 ; i < arr.length; i++) { if (arr[i]<min) min=arr[i]; } for ( int i = 0 ; i < arr.length; i++) { if (arr[i]% 2 == 0 ) oushu+= 1 ; else jishu+= 1 ; } System.out.println( "max=" +max+ ",min=" +min+ ",奇数个数" +jishu+ ",偶数个数" +oushu); } } |