1. package com.itheima.demo;
    2. public class Test1 {
    3. public static void main(String[] args) {
    4. // 目标: 数组元素求最值
    5. // 1.定义一个初始化数组,存储一批颜值
    6. int[] faceScore = {15,20,340,44,-5};
    7. // 2.定义一个变量用于存储最大元素,建议用第一个元素作为参考(因为要用for循环遍历,然后用数组里面的每一个元素和参考元素对比)
    8. int max = faceScore[0];
    9. // 3. 用for循环遍历数组的每个元素,依次与最大值变量的数据比较,若较大,则替换
    10. for (int i = 0; i < faceScore.length; i++) { //要多习惯用arr.length可以将数组里面的元素都遍历
    11. if (faceScore[i] > max) {
    12. max = faceScore[i];
    13. }
    14. }
    15. System.out.println("最大值为:" + max);
    16. }
    17. }
    1. package com.itheima.demo;
    2. import java.util.Random;
    3. import java.util.Scanner;
    4. public class Test2 {
    5. public static void main(String[] args) {
    6. // 需求:5个 1-20之间的随机数,让用户猜测,猜中要要提示猜中,还要输入当前的数组第一次出现的索引,并打印内容出来
    7. // 没有猜中就继续
    8. int[] data = new int[5]; // 由于不知道当前要存储哪些数据,所有要用动态数组
    9. Random r = new Random(); //
    10. for (int i = 0; i < data.length; i++) {
    11. data[i] = r.nextInt(1,21); // 随机生成1-20之间的数值,然后赋值给动态数组
    12. }
    13. // for (int i = 0; i < data.length; i++) {
    14. // System.out.println(data[i]); // 可以用for循环遍历动态数组,查看被赋予的值(配合arr.length)
    15. // }
    16. Scanner sc = new Scanner(System.in); // 定义一个用用户输入的API
    17. // 由于不知道用户猜多少次,所以用死循环
    18. OUT: // 这里的OUT相当于标识,结束循环时,会从这里开始结束
    19. while (true) {
    20. System.out.println("请输入你猜的数字:");
    21. int user_guess = sc.nextInt();
    22. // 用for循环将数组里面的元素遍历出来:将每个元素和用户输入的数字对比
    23. for (int i = 0; i < data.length; i++) {
    24. if (user_guess == data[i]){
    25. System.out.println("恭喜你,猜对了,你的索引是:" + i + "你的数字是:" + data[i]);
    26. // 猜中之后结束循环
    27. break OUT; // break只能结束当前的for循环,要用break OUT做表示,然后再while也做一个OUT标识,就能结束整个循环
    28. }
    29. }
    30. System.out.println("你输入的数字有误,请重新输入");
    31. }
    32. // 5.输出数组全部元素,让用户看到自己猜中的数字
    33. for (int i = 0; i < data.length; i++) {
    34. System.out.print(data[i] + "\t"); // 查看自己数字的索引,就可以知道自己猜的数字
    35. }
    36. }
    37. }
    1. package com.itheima.demo;
    2. import java.util.Random;
    3. import java.util.Scanner;
    4. public class Test3 {
    5. public static void main(String[] args) {
    6. // 目标:键盘录入一组工号,最终要随机一组出来作为排名
    7. // 1. 动态初始化一个数组,存储5个工号 // 使用了动态初始化数组,一定要用for循环将值赋给动态数组元素
    8. int[] codes = new int[5]; // 也可以用静态,但是会写死,用动态可以让用户输入
    9. // 2.定义一个循环,循环5次,依次录入工号存入对应的位置
    10. Scanner sc = new Scanner(System.in);
    11. for (int i = 0; i < codes.length; i++) {
    12. // 正式输入工号
    13. System.out.println("请输入第" + (i + 1) + "个工号"); // 计算机的索引是从0开始的所以我们要i + 1
    14. int code = sc.nextInt();
    15. // 将每次输入的值 存入到数组中
    16. codes[i] = code;
    17. }
    18. // 遍历数组中的每个元素,然后随机一个索引出来,让该元素与随机索引位置处的元素值进行交换(本节课的重点)
    19. // 使用Random类,用来随机索引
    20. Random r = new Random();
    21. for (int i = 0; i < codes.length; i++) {
    22. // 遍历当前的元素值:codes[i]
    23. // 随机一个索引位置出来:codes[index[
    24. int index = r.nextInt(codes.length);// 随机数的范围是0 ~ 4(不包含bound)写codes.length是为了不把他写死(就是5)
    25. int temp = codes[index]; // 定义一个临时变量用来存储随机索引的值
    26. codes[index] = codes[i]; // 让遍历出来的元素与随机索引交换
    27. codes[i] = temp; // 将最刚开始存储的随机索引的值,赋值给 遍历出来的元素, 这样就实现了交换
    28. }
    29. System.out.println("最新的排序结果为:");
    30. // 使用for循环遍历动态数组,查看最新的排序结果
    31. for (int i = 0; i < codes.length; i++) {
    32. System.out.print( codes[i] + "\t");
    33. }
    34. }
    35. }

    image.png

    1. package com.itheima.demo;
    2. public class Test4 {
    3. public static void main(String[] args) {
    4. // 1.定义一个数组,存储一些数据
    5. int[] arr = {5,2,3,1}; //冒泡排序,查看第一个数是否大于第二个数,大于则交换 (大的数向后排)
    6. // 0 1 2 3 // 第一轮索引0和1比,,索引1和索引2比,索引2和索引3比
    7. // 第二轮索引0和1比,索引1和索引2比 (因为大的向后排,所以不用管后面的数
    8. // 第三轮索引0和1比
    9. // 2.定义一个循环控制比较的轮数
    10. for (int i = 1; i < arr.length; i++) { // int i = 1;是比较的轮数,从第1轮开始
    11. // i == 1 比较的次数是 3 j == 0 1 2 (内部控制占三个位)
    12. // i == 2 比较的次数是 2 j == 0 1 (比较索引0 和1 的数)
    13. // i == 3 比较的次数是 1 j == 0
    14. // 3 .定义一个循环控制比较的次数,占位
    15. for (int j = 0; j < arr.length - i; j++) { // arr.length是定义比较的次数
    16. // 判断j当前位置的元素值,是否大于后一个位置,若较大则交换
    17. if (arr[j] > arr[j + 1]){
    18. int temp = arr[j];
    19. arr[j] = arr[j + 1];
    20. arr[j + 1] = temp;
    21. }
    22. }
    23. }
    24. // 输出排序后的结果
    25. for (int i = 0; i < arr.length; i++) {
    26. System.out.print(arr[i] + "\t");
    27. }
    28. }
    29. }