zoukankan      html  css  js  c++  java
  • 第六次上机作业

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

     1 package test2;
     2 
     3 public class test2 {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7         int [] arr=new int[] {10,20,30,40,50};
     8         for(int i = 0;i < arr.length;i++) {
     9             System.out.print(arr[i] + " ");
    10         }
    11 
    12     }
    13 
    14 }

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

     1 package test2;
     2 
     3 public class test2 {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7         int  arr[]=new int[5]  ;
     8          arr [0] = 10;
     9          arr [1] = 20;
    10          arr [2] = 30;
    11          arr [3] = 40;
    12          arr [4] = 50;
    13         for(int i = 0;i < arr.length;i++) {
    14             System.out.print(arr[i] + " ");
    15         }
    16 
    17     }
    18 
    19 }

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

     1 package test2;
     2 
     3 public class test2 {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7         int  arr[]=new int[] {23,45,22,33,56};
     8         int sum = 0;
     9         int a;
    10         int average = 0;
    11         for(a = 0;a < arr.length;a++) { 
    12             sum +=arr[a];
    13             average = sum/5;
    14         }
    15          System.out.println("和为:"+sum+"平均值为:"+average);
    16 
    17     }
    18 
    19 }

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

     1 package test2;
     2 
     3 public class test2 {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7         int arr[]= {18,25,7,36,13,2,89,63};
     8                                 int max=arr[0];
     9                                 int index=0;
    10                                 int i;
    11                                     for(i = 1;i < arr.length;i++) {
    12                                          if(max < arr[i]) {
    13                                                max = arr[i];
    14                                                index = i;
    15                           }
    16                   }
    17                           System.out.println("最大数为"+max+"下标为"+index);
    18     
    19             }
    20      }

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

     1 package test2;
     2 
     3 public class test2 {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7          int arr[] = new int[] {43,26,3,43,11};
     8             for (int a = 4; a >= 0; a--) {
     9             
    10                 System.out.println(" "+arr[a]);
    11             }
    12     }
    13 }
  • 相关阅读:
    WRF WPS预处理
    CVS安装
    Linux窗口最小化消失,任务栏上无法找到的解决方法
    NCARG安装配置出现error while loading shared libraries: libg2c.so.0问题额解决办法
    Netcdf安装
    Embedding R-generated Interactive HTML pages in MS PowerPoint(转)
    The leaflet package for online mapping in R(转)
    Some 3D Graphics (rgl) for Classification with Splines and Logistic Regression (from The Elements of Statistical Learning)(转)
    What does a Bayes factor feel like?(转)
    Weka算法介绍
  • 原文地址:https://www.cnblogs.com/919753740yu/p/12665968.html
Copyright © 2011-2022 走看看