zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    大数据量业务订制和解决方案思考
    gc内存回收机制
    HashMap的扩容机制, ConcurrentHashMap和Hashtable主要区别
    开源工作流引擎内核主要关心的是什么?
    mysql查询计划
    mysql 存储引擎MYSIAM和INNODB特性比较
    任意输入字符,对字符匹配进行判断
    java 对list中对象按属性排序
    mysql 写数据操作几次硬盘?
    win7_64位+U盘制作centos6.3+安装centos
  • 原文地址:https://www.cnblogs.com/qq1123514689/p/12665295.html
Copyright © 2011-2022 走看看