(二)java循环、一维数组

1. 上节课的补充(循环)

循环语法特征如果初始条件不成立使用场合
Whilewhile(循环条件){//循环操作}先判定后执行循环一次都不执行万能循环
do -whiledo{//循环操作}while(循环条件)先执行后判定执行一次对于先执行后判定的题目
forfor(循环变量定义;循环条件;循环变量的改变){//循环操作}先判定后执行循环一次都不执行循环次数固定的题目

1.1 嵌套循环

1.1.1 打印矩阵

public class Test4 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 6; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

1.1.2 打印直角三角形

public class Test5 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

1.1.3 打印等腰三角形

public class Test6 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int k = 1; k <= 5 - i; k++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

    }
}

2. 一维数组概念

数组(Array),是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据进行统一管理
变量:在内存开辟一块空间
数组:也是变量,在内存中开辟一串连续的空间,数组长度一旦确定,就不能修改数组的常见概念:

  • 数组名
  • 数组下标(索引)
  • 元素
  • 数组的长度

3. 一维数组基本语法

3.1 格式

  • 格式1.数据类型[] 数组名,属于只创建了数组引用名,并未在内存创建数组空间
  • 格式2.数据类型[] 数组名 = new 数据类型[数组长度];
  • 格式3.数据类型[] 数组名 = {数组内容1,数组内容2,数组内容3,数组内容4}
  • 格式4. 数据类型[] 数组名 = new 数据类型[]{内容1,内容2,内容3,内容4}

3.2 代码

import java.util.Arrays;

public class Test7 {
    public static void main(String[] args) {
        int[] arr1;//只声明,没有开辟内存空间
        arr1 = new int[4];//开辟存储空间
        double[] arr2 = new double[5];
        String[] arr3;
        int[] arr4 = new int[]{1, 2, 3, 4};
        System.out.println(arr4);
        System.out.println(Arrays.toString(arr4));
        double[] arr5 = {1.5, 3.6, 8, 4.0};
        System.out.println(Arrays.toString(arr5));
    }
}

4. 一维数组内存结构

JMM
jvm的内存结构
当执行到int[] arr1;内存状态为:
在这里插入图片描述
执行arr1 = new int[4];
在这里插入图片描述
执行下面的代码

int[] ages = new int[4]
ages[0] = 12;
ages[1] = 23;
ages[2] = 33;
String[] names = new String[]{"赵宇","张恺","江运","曹林"}
names[1] = "刘昭"
names = new String[]{"Tom","Jerry"};
sysout(names[0]);

在这里插入图片描述

5. 一维数组数组的实战

5.1 读取数组

取出数组中的值:数组名称【下标索引】

public class Test8 {
    public static void main(String[] args) {
        int[] arr1 = new int[4];
        for (int i = 0; i < arr1.length; i++) {
            arr1[i] = i + 1;
        }
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);

        }
    }
}

5.2 数组默认值

public class Test9 {
    public static void main(String[] args) {
        int[] arr1 = new int[4];
        //int类型数组默认值
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);
        }
        //double类型数组默认值
        double[] arr2 = new double[4];
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
        //String类型数组默认值
        String[] name = new String[4];
        for (int i = 0; i < name.length; i++) {
            System.out.println(name[i]);
        }
        //char类型数组系统默认值
        char[] gender= new char[5];
        for (int i = 0; i < gender.length; i++) {
            System.out.println(gender[i]);

        }
        //boolean类型数组元素的默认值为false
        System.out.println("over");

    }
}

5.3 求极值问题

public class Test10 {
    public static void main(String[] args) {
        int[] arr = {12, 43, -45, 567, 13};
        int max = arr[0];
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        System.out.println("最大值为" + max);
        System.out.println("最小值为" + min);
    }
}

6. 一维数组练习

从键盘读入学生成绩,找出最高分,并输出学生成绩等级

  • 成绩>=最高分-10 等级为‘A’
  • 成绩>=最高分-20 等级为‘B’
  • 成绩>=最高分-30 等级为‘C’
  • 其余 等级为‘D’
    提示:先读入学生人数,根据人数创建int数组,存放学生成绩

结果展示:
在这里插入图片描述
代码如下:

import java.util.Scanner;

public class Test11 {//成绩划分

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        //键入学生人数
        System.out.println("请输入学生人数");
        int personCount = scanner.nextInt();

        //键入同学成绩
        double[] personScore = new double[personCount];
        System.out.println("请输入" + personCount + "个同学的成绩");
        for (int i = 0; i < personScore.length; i++) {
            personScore[i] = scanner.nextDouble();
        }
        //查找最高分
        double maxScore = personScore[0];
        for (int i = 1; i < personScore.length; i++) {
            if (maxScore < personScore[i]) {
                maxScore = personScore[i];
            }
        }
        System.out.println("这" + personCount + "个学生的最高分为" + maxScore);
        for (int i = 0; i < personScore.length; i++) {
            if (maxScore - personScore[i] <= 10) {
                System.out.println("学生" + i + "的成绩是" + personScore[i] + " 划分为\"A\" ");
            } else if (maxScore - personScore[i] <= 20) {
                System.out.println("学生" + i + "的成绩是" + personScore[i] + " 划分为\"B\" ");
            } else if (maxScore - personScore[i] <= 30) {
                System.out.println("学生" + i + "的成绩是" + personScore[i] + " 划分为\"C\" ");
            } else {
                System.out.println("学生" + i + "的成绩是" + personScore[i] + " 划分为\"D\" ");
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值