第六周上机作业
1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
1 package test2;
2
3 public class test2 {
4
5 public static void main(String[] args) {
6 // TODO Auto-generated method stub
7 int [] arr=new int[] {10,20,30,40,50};
8 for(int i = 0;i < arr.length;i++) {
9 System.out.print(arr[i] + " ");
10 }
11
12 }
13
14 }
2.编写一个简单程序,要求数组长度为5,动态赋值10,20,30,40,50,在控制台输出该数组的值
1 package test2;
2
3 public class test2 {
4
5 public static void main(String[] args) {
6 // TODO Auto-generated method stub
7 int arr[]=new int[5] ;
8 arr [0] = 10;
9 arr [1] = 20;
10 arr [2] = 30;
11 arr [3] = 40;
12 arr [4] = 50;
13 for(int i = 0;i < arr.length;i++) {
14 System.out.print(arr[i] + " ");
15 }
16
17 }
18
19 }
3.编写一个简单程序,定义整型数组,里面的元素是{23,45,22,33,56},求数组元素的和、平均值
1 package test2;
2
3 public class test2 {
4
5 public static void main(String[] args) {
6 // TODO Auto-generated method stub
7 int arr[]=new int[] {23,45,22,33,56};
8 int sum = 0;
9 int a;
10 int average = 0;
11 for(a = 0;a < arr.length;a++) {
12 sum +=arr[a];
13 average = sum/5;
14 }
15 System.out.println("和为:"+sum+"平均值为:"+average);
16
17 }
18
19 }
4.在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。
1 package test2;
2
3 public class test2 {
4
5 public static void main(String[] args) {
6 // TODO Auto-generated method stub
7 int arr[]= {18,25,7,36,13,2,89,63};
8 int max=arr[0];
9 int index=0;
10 int i;
11 for(i = 1;i < arr.length;i++) {
12 if(max < arr[i]) {
13 max = arr[i];
14 index = i;
15 }
16 }
17 System.out.println("最大数为"+max+"下标为"+index);
18
19 }
20 }
5. 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
1 package test2;
2
3 public class test2 {
4
5 public static void main(String[] args) {
6 // TODO Auto-generated method stub
7 int arr[] = new int[] {43,26,3,43,11};
8 for (int a = 4; a >= 0; a--) {
9
10 System.out.println(" "+arr[a]);
11 }
12 }
13 }


浙公网安备 33010602011771号