• 4.9上机作业


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

    package dome;

     
    import java.util.Scanner;
     
    public class Dome4 {
        public static void main(String[] args) {       
          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 a;

    import java.util.*;

    public class aa {

    public static void main(String[] args) {
    // TODO Auto-generated method stub;
    int[] a = new int[6];
    for (int i = 1; i < a.length; i++) {
    a[i]+=a[i-1]+10;
    System.out.print(a[i] + " ");
    }
    }
    }

     

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

    package dome;

     
    public class Dome4 {
        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)的数组中找出其中最大的数及其下标。

    package a;

    import java.util.*;

    public class aa {

    public static void main(String[] args) {
    // TODO Auto-generated method stub;
    int[] a = new int[] { 18, 25, 7, 36, 13, 2, 89, 63 };
    int max = a[0];
    for (int i = 0; i < a.length; i++) {
    if (max < a[i]) {
    max = a[i];
    }
    }
    System.out.println("最大值是:" + max);
    int count = 0;
    for (int i = 0; i < a.length; i++) {
    if (a[i] == max) {
    System.out.println("下标值是:" + count);
    }
    count++;
    }
    }
    }

    5. 将一个数组中的元素逆序存放

     复制代码

    package wang;
    
    public class li {
    
    public static void main(String[] args){
                 
                      
         int[] a = {8,1,2,3,4};
                      
         int m;
         for(int i = 0; i < 2; i++){
                      
           m = a[i];
           a[i] = a[5-i-1];
           a[5-i-1] = m;
                     
     }
        for(int j =0; j < 5; j++){
         System.out.println(a[j]);
         }
                     
      }
                    
    }
    复制代码
  • 相关阅读:
    Java开发web的几种开发模式
    Tomcat7.0安装配置
    Apache与Tomcat 区别联系
    编写 Window 服务程序
    系列文章--SharePoint 开发教程
    Google Chrome浏览器调试功能介绍
    Chrome的JS调试工具
    Google Chrome 调试JS简单教程[更新]
    ASP.NET常用标准配置web.config
    WINDOWS下kill进程的命令
  • 原文地址:https://www.cnblogs.com/immortals0/p/12665418.html
Copyright © 2020-2023  润新知