4.9 上机作业

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

package zy;

public class hj {

    public static void main(String[] args) {
        int arr [] ={10,20,30,40,50};

        for(int i = 0;i){
            System.out.println(arr[i]);
        }
    }

}

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

package zy;
import java.util.Scanner;
public class hj {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入数字");
         int ar [] = new int [5];
         for(int i=0;i){
             ar[i] = sc.nextInt();
         }
         System.out.println("赋值结束");
               
         }
     }

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

package zy;
import java.util.Scanner;//
public class hj {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);//
         int arr[]={23,45,22,33,56};
            double sum=0;
            for(int i=0;i) {
                sum+=arr[i];
            }
            System.out.println("和为"+sum+"平均值为"+sum/5);
        }
    }

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

package zy;
import java.util.Scanner;//
public class hj {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);//
        int arr[]={18,25,7,36,12,2,89,63};
        int max=arr[0];
        int xb=0;
        int i=0;
        for(i=0;i) {
            if(max<arr[i]) {
                max=arr[i];
                xb=i;
            }
        }
        System.out.println("最大值为"+max+"其下标为"+xb);
    }
}

 

5.将一个数组中的元素逆序存放

package zy;

public class hj {

    public static void main(String[] args) {

        int [] a = new int [] {6,1,3,5,10,2,66};
        int t;
        for(int i = 0; i < a.length / 2; i ++) {
            t = a[i];
            a[i] = a[a.length - 1 - i];
            a[a.length - 1 - i] = t;
        }
        for(int i = 0; i < a.length; i ++) {
            System.out.print(a[i] + ", ");
        }
    }
}

 

你可能感兴趣的:(4.9 上机作业)