• 第六周上机作业


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

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

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

    package jxk;
    import java.util.Scanner;
    public class se {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input=new Scanner(System.in);
            int[] a=new int[5];
            a[0]=10;
            System.out.println(a[0]);
            for(int i=1;i<5;i++){
                a[i]+=a[i-1]+10;
                  System.out.println(a[i]);
               }
            }
        }

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

    package jxk;
    
    public class Der {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] a ={23,45,22,33,56};
            int sum=0,i;
            float b;
            for(i=0;i<5;i++){
                sum+=a[i];
            }
            b=sum/5;
                 System.out.println("这个数组的和是"+sum);
                 System.out.println("这个数组的平均值是"+b);
            }
    }

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

    package jxk;
    
    public class Der {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] a = new int[] {18,25,7,36,13,2,89,63};
            int maxnum = 0,
                maxnumxia = 0;
            for(int i = 0;i < a.length;i++) {
                if(maxnum < a[i]) {
                    maxnum = a[i];
                    maxnumxia = i;
                }
            }
            System.out.println("数组中最大数是:"+maxnum +
             " 其在数组中的下标是:" + maxnumxia);
        }
    }

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

    package jxk;
    import java.util.Scanner;
    public class se {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
             Scanner input = new Scanner(System.in);
             System.out.println("请输入数组长度"); 
             int n= input.nextInt(); 
             int a[]=new int[n]; 
             System.out.println("请输入数组数据");
             for(int i=0;i<a.length;i++){ a[i]=input.nextInt();
             } int temp;
             for(int i=0;i<a.length/2;i++){ 
                 temp=a[i]; a[i]=a[a.length-i-1];
                 a[a.length-i-1]=temp;
                 } System.out.println("这个数组的逆序存放是");
                 for(int i=0;i<a.length;i++) { 
                     System.out.print(a[i]+" "); 
                 }
            }
        }

    6、有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。(知识点:数组遍历、数组元素访问)

    package jxk;
    import java.util.Arrays;
    import java.util.Scanner;
    public class deom {
        @SuppressWarnings("resource")
        public static void main(String[] args) {
            int[] array = { 1, 5, 9, 42, 35, 36, 47, 58, 76, 10 };// 定义一个升序排序数组
            //int[] array = { 10, 76, 58, 47, 36, 35, 42, 9, 5, 1 };// 定义一个降序排序数组
            System.out.print("定义的数组为:");
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + " ");
            }
            System.out.println();
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入一个数:");
            int addNumber = sc.nextInt();
            array = Arrays.copyOf(array, array.length + 1);// 数组扩容
            array[array.length - 1] = addNumber; // 暂时将数组的最后一个数赋值为插入的数
            //判断数组是降序还是升序,对应其规律进行输出
            int i = 0;
            //当定义的数组为升序的时候
            if (array[i] <= array[array.length - 2]) {
                Arrays.sort(array);// 再次排序
                System.out.print("插入一个数后的数组为:");
                for (i = 0; i < array.length; i++) {
                    System.out.print(array[i] + " ");
                }
            } else {//当定义的数组为降序的时候,数组进行倒序输出
                Arrays.sort(array);// 再次排序
                System.out.print("插入一个数后的数组为:");
                for (i = array.length - 1; i >= 0; i--) {
                    System.out.print(array[i] + " ");
                }
            }
        }
    }

  • 相关阅读:
    iScroll.js 用法参考
    行内元素和块级元素
    struct和typedef struct彻底明白了
    C/C++语法知识:typedef struct 用法详解
    不是技术牛人,如何拿到国内IT巨头的Offer (转载)
    笔试客观题-----每天收集一点点
    <C++Primer>第四版 阅读笔记 第一部分 “基本语言”
    <C++Primer>第四版 阅读笔记 第四部分 “面向对象编程与泛型编程”
    <C++Primer>第四版 阅读笔记 第三部分 “类和数据抽象”
    <C++Primer>第四版 阅读笔记 第二部分 “容器和算法”
  • 原文地址:https://www.cnblogs.com/linshuai3/p/12666036.html
Copyright © 2020-2023  润新知