zoukankan      html  css  js  c++  java
  • 程序设计中临时变量的使用(课下测试,选做)

    1.作业要求

    提交:
        编译运行没有问题后,git add .  git commit -m "数组元素删除,插入"  git push;     提交码云上你完成的代码的链接。

    任务:
      //定义一个数组,比如
      int arr[] = {1,2,3,4,5,6,7,8};

      //打印原始数组的值
      for(int i:arr){
          System.out.print(i + " ");
      }
      System.out.println();
     
      // 添加代码删除上面数组中的5
      ...
     
      //打印出 1 2 3 4 6 7 8 0
      for(int i:arr){
          System.out.print(i + " ");
      }
      System.out.println();

      // 添加代码再在4后面5
      ...
     
      //打印出 1 2 3 4 5 6 7 8
      for(int i:arr){
          System.out.print(i + " ");
      }
      System.out.println();

    2.代码如下:

    public class PrintArrays {
        public static void main(String[] args) {
           
            //定义一个数组,比如
            int temp = -1;
            int arr[] = {1,2,3,4,5,6,7,8};
    
            //打印原始数组的值
            for(int i:arr){
                System.out.print(i + " ");
            }
            System.out.println();
    
            //从数组中删除5
            for(int i:arr){
                if(arr[i] == 5){
                    temp = i;
                    break;
                }
            }
            for(int i=temp+1;i<arr.length;i++){
                arr[i-1] = arr[i];
            }
            arr[arr.length-1] = 0;
    
            //打印出 1 2 3 4 6 7 8 0
            for(int i:arr){
                System.out.print(i + " ");
            }
            System.out.println();
    
            // 添加代码再在4后面5
            for(int i:arr){
                if(arr[i] == 4){
                    temp = i;
                    break;
                }
            }
            for(int i=arr.length-1;i>temp+1;i--){
                arr[i] = arr[i-1];
            }
            arr[temp+1] = 5;
            
            //打印出 1 2 3 4 5 6 7 8
            for(int i:arr){
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }

    3.运行结果

    码云链接(https://gitee.com/zxjacdl/zxj175221/tree/master/PrintArrays)

  • 相关阅读:
    php内存管理机制、垃圾回收机制
    Redis 3.2.1集群搭建
    centos开启IPV6配置方法
    /etc/hosts.allow和/etc/hosts.deny详解
    3元购买微信小程序解决方案一个月
    linux下使用ntfs-3g挂载NTFS出错
    腾讯云微信小程序域名变更指南
    nginx开启gzip压缩
    centos 7使用yum安装docker容器
    linux中启动网卡报错:Bringing up interface eth1: Error: Connection activation failed
  • 原文地址:https://www.cnblogs.com/zxja/p/10747212.html
Copyright © 2011-2022 走看看