4.9作业

1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。

 1 package pro1;
 2 
 3 public class test {
 4     public static void main(String[] args) {
 5         int a[]= {10,20,30,40,50};
 6         for(int i=0;i<a.length;i++) {
 7             System.out.println(a[i]);
 8         }
 9     }
10 }


2.编写一个简单程序,要求数组长度为5,动态赋值10,20,30,40,50,在控制台输出该数组的值。

 1 package pro1;
 2 
 3 public class test {
 4     public static void main(String[] args) {
 5         int a[]= new int[5];
 6         a[0]=10;
 7         a[1]=20;
 8         a[2]=30;
 9         a[3]=40;
10         a[4]=50;
11         for(int i=0;i<a.length;i++) {
12             System.out.println(a[i]);
13         }
14     }
15 }


3.编写一个简单程序,定义整型数组,里面的元素是{23,45,22,33,56},求数组元素的和、平均值。

 1 package pro1;
 2 
 3 public class test {
 4     public static void main(String[] args) {
 5          int a[]= {23,45,22,33,56};
 6             double sum=0;
 7             for(int i=0;i<a.length;i++) {
 8                 sum+=a[i];
 9             }
10             System.out.println("和为"+sum+"平均值为"+sum/5);
11     }
12 }


4.在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。

 1 package pro1;
 2 
 3 public class test {
 4     public static void main(String[] args) {
 5         int a[]= {18,25,7,36,13,2,89,63},max=a[0],b=0;
 6         for(int i=1;i<a.length;i++) {
 7             if(max < a[i]) {
 8                 max = a[i];
 9                 b = i;
10             }
11         }
12         System.out.println("最大数为"+max+"下标为"+b);
13     }
14 }

5. 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)

 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         Scanner in = new Scanner(System.in);
 6         int N = in.nextInt();
 7         int a[] = new int[N];
 8         for (int i = 0; i < a.length; i++) {
 9             a[i] = in.nextInt();
10         }
11         int temp;
12         for (int i = 0; i < a.length / 2; i++) {
13             temp = a[i];
14             a[i] = a[a.length - i - 1];
15             a[a.length - i - 1] = temp;
16         }
17         for (int i = 0; i < a.length; i++) {
18             System.out.println(a[i] + " ");
19         }
20     }
21 }

 

posted @ 2020-04-09 10:49  潘宇航  阅读(189)  评论(1编辑  收藏  举报