zoukankan      html  css  js  c++  java
  • 第5上机练习~~~~~~~~~~~~~~~~~~~~~~~~~~~~武汉一定要加油!!!

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

    package kl;
    
    public class suk {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int arr[]= {10,20,30,40,50};
            for(int i=0;i<5;i++) 
            {
                System.out.println(arr[i]);
            }
    }
    }

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

    package kl;
    
    public class suk {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int [] arr=new int [5];
            arr[0]=10;
            arr[1]=20;
            arr[2]=30;
            arr[3]=40;
            arr[4]=50;
            for(int i=0;i<arr.length;i++) {
                System.out.println(arr[i]);
            }
    }
    }

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

    package kl;
    
    public class suk {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int [] arr = new int [] {23,45,22,33,56};
            int sum=0;
            int everage=0;
            for(int i=0;i<arr.length;i++) {
                sum=sum+arr[i];    
            }
            everage=sum/5;
            System.out.println("数组和为:"+sum+"数组平均值为:"+everage);
    }
    }

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

    package kl;
    public class suk{
         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. 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)

    package kl;
    
    public class suk {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub    
                int [] arr =new int [] {18,25,7,36,13,2,89,63};
                int [] brr =new int [8];
                int i;
                for(i=0;i<arr.length;i++) {
                    brr[i]= arr[arr.length-1-i];
                    System.out.print(brr[i]+",");
                    }
        }
    }

  • 相关阅读:
    JAVA的学习日记15
    JAVA的学习日记14
    CIRD主站与DOPAS构建笔记#1
    信仰之题——Codeforces Round 259(附题面完整翻译)
    平面最近点对问题
    BZOJ4552 [Tjoi2016&Heoi2016]排序
    BZOJ1001 [Beijing2006]狼抓兔子
    (二)k8s编写资源清单
    linux常用搜索工具find/whereis/locate
    解决centos7 的/etc/rc.local不会开机执行
  • 原文地址:https://www.cnblogs.com/skyfail/p/12666063.html
Copyright © 2011-2022 走看看