zoukankan      html  css  js  c++  java
  • 第四次上机

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

    package come.itheima01;
    import java.util.Scanner;
    public class Sz {
        public static void main(String[] args) {
            
            int[] arr = new int[] { 10, 20, 30, 40, 50 };
            for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
            }
            }
            }

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

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

    int[] a = new int[]{23,45,22,33,56};
    int sum=0,pj=0;
    for (int i = 0; i < a.length; i++) {
    sum+=a[i];
    }
    pj=sum/5;
    System.out.println(sum + " " + pj);
    }
    }

     

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

    package come.itheima01;
    import java.util.Scanner;
    public class Sz {
        public static void main(String[] args) {
            
            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 come.itheima01;
    
    public class Sz {
        public static void main(String[] args) {
              
                      int[] grade = {87,88,89,98,78};
              
                      int m;
                      for(int i = 0; i < 2; i++){
              
                         m = grade[i];
                         grade[i] = grade[5-i-1];
                         grade[5-i-1] = m;
             
                     }
                     for(int j =0; j < 5; j++){
                         System.out.println(grade[j]);
                     }
             
                 }
             
             }

  • 相关阅读:
    Pythontutor:可视化代码在内存的执行过程
    katalon系列六:Katalon Studio Web UI关键字讲解
    chrome flash插件改为自动运行
    katalon系列五:使用Katalon Studio手动编写WEB自动化脚本
    JMeter随机上传附件
    JDK11安装后,环境变量的坑
    katalon系列四:使用Katalon Studio录制WEB自动化脚本
    katalon系列三:Project Setting-项目设置
    tomcat服务器上https的SSL证书安装配置
    eclipse安装freemarker插件
  • 原文地址:https://www.cnblogs.com/songzhuo/p/12665469.html
Copyright © 2011-2022 走看看