zoukankan      html  css  js  c++  java
  • java第七次作业

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

    package one;
    
    public class one1 {
        public static void main(String[] args) {
            int[]arr = new int[]{10,20,30,40,50};
            for(int i = 0;i<arr.length;i++){
                System.out.println(arr[i]+" ");
            }
        }
    }

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

    package one;
    
    public class one1 {
        public static void main(String[] args) {
            int a[] = new int [5];
            a[0]=10;
            a[1]=20;
            a[2]=30;
            a[3]=40;
            a[4]=50;
            for(int i = 0;i<a.length;i++){
                System.out.println(a[i]);
            }
        }
    }

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

    package one;
    
    public class one1 {
        public static void main(String[] args) {
            int a[] = {23,45,22,33,56};
            double sum = 0;
            for(int i = 0;i<a.length;i++){
                sum+=a[i];
            }
            System.out.println("和为: "+sum+"平均值为: "+sum/5);
        }
    }

    4..在一个有8个整数(18257361328963)的数组中找出其中最大的数及其下标。

    package one;
    
    public class one1 {
        public static void main(String[] args) {
            int[]arr = {18,25,7,36,13,2,89,63};
            int max = arr[0];
            int maxIndex = 0;
            for(int i = 1;i<arr.length; i++){
                if(max<=arr[i]){
                    max = arr[i];
                    maxIndex = i;
                }
            }
            System.out.println("最大值为: "+max+"
    最大值下表为: "+maxIndex);
        }
    }

    5. 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问)

    package one;
    import java.util.Scanner;
    public class one1 {
        public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
            int arr[]={34,23,45,67,5};
            int j;
            for(int i=0;i<2;i++){
                j=arr[i];
                arr[i]=arr[4-i];
                arr[4-i]=j; 
            }
            for(int a=0;a<5;a++){
                
                System.out.println(arr[a]);
            }
        }
    
    }
  • 相关阅读:
    ssm框架中的struts我的配置问题
    ssm框架web.xml中filter配置问题
    partition-list
    entity.Student@150f3932, entity.Student@1a740c6b 没有实体中的数据
    UVA 11361 Investigating Div-Sum Property
    UVA 10883 Supermean
    Gym 101081K Pope's work
    UVA 1103 How Many O's?
    HOJ 1108
    HDU 5936 朋友
  • 原文地址:https://www.cnblogs.com/zjzj123/p/12665671.html
Copyright © 2011-2022 走看看