• 上机练习(4.9)


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

    public class zxcvbn {
    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.print(arr[i]+" ");
    }
    }

    }

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

    public class zxcvbn {
    public static void main(String[] args) {
                   int[] buffer=new int[5];

                   buffer[0]=10;
                   buffer[1]=20;
                    buffer[2]=30;
                    buffer[3]=40;
                    buffer[4]=50;

                    for(int i=0;i<5;i++)
    {
    System.out.println(buffer[i]);
    }
    }
    }

     

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

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

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

    public class zxcvbn {

    public static void main(String[] args) {
                       int[] aa = {18,25,7,36,13,2,89,63};
                       int[] bb = new int[aa.length];
                       System.arraycopy(aa, 0, bb, 0, aa.length);

                      for(int y=0; y<bb.length-1; y++) {
                      if(bb[y]>bb[y+1]) {
                     int temp = bb[y];
                     bb[y] = bb[y+1];
                    bb[y+1] = temp;
              }
         }
                   for(int element : bb) {
                  System.out.print(element+" ");
          }
                   System.out.println("-------");
                   System.out.println("这个数组最大的数是:"+ bb[bb.length-1]);
                   int xiabiao = 0;

                  for(int i=0; i<aa.length; i++) { //遍历寻找对应的数的下标
                 if(aa[i]==bb[bb.length-1]) {
                  xiabiao = i;
               }
          }
                   System.out.println(xiabiao);
              }
    }

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

    public class zxcvbn {
    public static void main(String[] args) {
    int a[] = new int[]{17, 26, 3, 99, 63};
    for (int i = 0; i < a.length / 2; i++)
    {
    int b = a[i];
    a[i] = a[a.length - i - 1];
    a[a.length - i - 1] = b;
    }
    for (int i = 0; i < a.length; i++)
    {
    System.out.println(a[i]);
    }
    }

    }

  • 相关阅读:
    shell脚本按当前日期输出日志
    bat弹出确认或取消窗口
    bat脚本输出日志
    北京浩赢科技有限公司与好宝多多
    bat延迟执行脚本,利用choice实现定时执行功能
    centos下安装tunctl
    OpenStack Train版 简单部署流程(4)- octavia
    LVM基础
    OpenStack Train版 简单部署流程(3)- ceilometer
    openstack stein
  • 原文地址:https://www.cnblogs.com/fanbudufan/p/12665408.html
Copyright © 2020-2023  润新知