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

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