一、循环语句
- for 循环
- while 循环
- do-while 循环
1、for循环
语法格式:
public static void main(String[] args) {for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
- ②循环条件部分为boolean类型表达式,当值为false时,退出循环
- ①初始化部分可以声明多个变量,但必须是同一个类型,用逗号分隔
- ④可以有多个变量更新,用逗号分隔
2、while循环
语法格式:
public static void main(String[] args) {
int result = 0;
for (int i = 0; i <= 5; i++) {
if (i == 3) {
System.out.println(i);
continue;
}
result += i;
}
System.out.println("result总和为:" + result);
}
- 注意不要忘记声明④迭代部分。否则,循环将不能结束,变成死循环。
- for循环和while循环可以相互转换
3、do-while循环
public static void main(String[] args) {
// do while
int result = 0, a = 1;
do {
result += a;
a++;
} while (a < 5);
System.out.println("result总和为:" + result);
}
- do-while循环至少执行一次循环体。
练习:从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("请输入数字:");
double sendNum = scan.nextDouble();
if (sendNum == 0) {
System.out.println("结束");
break;
} else if (sendNum < 0) {
System.out.println("负数");
} else {
System.out.println("正数");
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/464fe9583e8214c183ef7b4bb8b20ec3.png)
多层嵌套:
public static void main(String[] args) {
label:
while (true) {
for (int i = 1; i < 1000; i++) {
System.out.println(i);
if (i == 5) {
System.out.println("结束循环:" + i);
break label; // 结束外层(while)循环,否则会一直循环
}
}
}
}
- break:终止当层循环;
- continue:跳出本次循环,继续下一次循环;
- 带标签的break或continue:在多层嵌套的循环语句体中时,可以通过标签指明要跳过的是哪一层循环
带标签的break和continue例子:
public static void main(String[] args) {
label:
while (true) {
labelFor:
for (int i = 1; i < 1000; i++) {
if (i == 8) {
System.out.println("结束循环:" + i);
break label; // 结束外层(while)循环,否则会一直循环
} else if (i == 6) {
System.out.println("continue:" + i);
continue labelFor; // continue跳出本次for循环
} System.out.println(i); } } }
输出:
1
2
3
4
5
continue:6
7
结束循环:8
二、数组
1、数组(Array),是多个相同类型数据按一定顺序排列的集合
- 数组本身是引用数据类型,而数组中的元素可以是任何数据类型,包括基本数据类型和引用数据类型。
- 创建数组对象会在内存中开辟一整块连续的空间,而数组名中引用的是这块连续空间的首地址。
- 数组的长度一旦确定,就不能修改。(特别要注意)
-
我们可以直接通过下标(或索引)的方式调用指定位置的元素,速度很快。
有点像Python的列表,但是区别还是有很大的。
java中不可以下标为负数,Python是可以的。java空指针nulll既是指向内存地址解析没存在初始值,它的值为:null,而Python的是None,输出的时候也是None,不存在空指针;
![](https://i-blog.csdnimg.cn/blog_migrate/d2ae6cd10adf74736615225d7c472251.png)
2、数组的默认初始化值
![](https://i-blog.csdnimg.cn/blog_migrate/1697610235a669eb7da40304e5ddd6ef.png)
数组中存在默认值的,如下:
public class ArrayDF {
public static void main(String[] args) {
int[] arr_int = new int[2];
boolean[] arr_bool = new boolean[2];
char[] arr_char = new char[2];
String[] arr_str = new String [2];
System.out.println(arr_int[1]);
System.out.println(arr_bool[1]);
System.out.println(arr_char[0]);
System.out.println(arr_str[1]);
}
}
输出:
0
false
null
3、一维数组
package com.array1219;
/*
@USER PPL-泡泡龙 or 广深小龙
@date 2020-12-19 14:04
*/
public class Array {
public static void main(String[] args) {
// 静态初始化
int[] numbers = new int[]{1, 2, 3, 4, 5};
int[] num = {1,2,3,4,5}
// 动态初始化
String[] names = new String[5]; names[0] = "泡泡龙"; names[1] = "广深小龙"; names[2] = "龙小龙"; names[3] = "PPL"; names[4] = "小龙"; // 获取数组长度 System.out.println("长度为:" + names.length); // 索引获取数组的值 System.out.println(names[0]); // 遍历数组 for (int i = 0; i < names.length; i++) { System.out.println(names[i]); } } }
一维数组的练习
package com.array1219;
/*
@USER PPL-泡泡龙 or 广深小龙
@date 2020-12-19 21:10
*/
import java.util.Scanner;
public class ArrayDemo {
public static void main(String[] args) {
// 定义初始值
int[] numList = new int[5];
int max = 0;
// 实例化键盘输入类
Scanner scan = new Scanner(System.in);
System.out.println("请输入学生5个成绩"); // 打印键盘输入提示
// 键盘输入值:循环 numList,共需要输入多少次,且判断最大值;
for (int i = 0; i < numList.length; i++) {
int num = scan.nextInt();
numList[i] = num;
if (numList[i] > max) {
max = numList[i];
}
}
// 再次遍历 numList ,根据 max 来进行分数评级;
for (int i = 0; i < numList.length; i++) {
if (numList[i] >= max - 10) {
System.out.println("分数:" + numList[i] + " 等级为:A");
} else if (numList[i] >= max - 20) {
System.out.println("分数:" + numList[i] + " 等级为:B");
} else if (numList[i] >= max - 30) {
System.out.println("分数:" + numList[i] + " 等级为:C");
} else {
System.out.println("分数:" + numList[i] + " 等级为:D");
}
}
}
}
输出:
请输入学生5个成绩
44
55
66
77
88
分数:44 等级为:D
分数:55 等级为:D
分数:66 等级为:C
分数:77 等级为:B
分数:88 等级为:A
4、二维数组
![](https://i-blog.csdnimg.cn/blog_migrate/e05a4bce0c9bb86b316020de8b7fa451.png)
2、二维数组初始值/遍历例子
package com.array1219;
/*
@USER PPL-泡泡龙 or 广深小龙
@date 2020-12-19 23:34
*/
public class ArrayMany {
public static void main(String[] args) {
// 静态初始化
int[][] number = new int[][]{{1, 2, 3}, {3, 4}, {4, 5, 6, 7, 8}};
int[][] num = {{1, 2, 3}, {}};
System.out.println(number[0][2]); // number下标0,则为第一组数据,[2]下标2的值
System.out.println(number[0].length);
System.out.println(number.length);
String[][] str = {{"1", "泡泡龙"}, {"2", "广深小龙"}};
System.out.println(str[0]); // 指向0数组的内存地址
// 遍历二维数组(多维数组就用多次循环)
for (int i = 0; i < str.length; i++) {
for (int j = 0; j < str[i].length; j++) {
System.out.print(str[i][j] + " ");
}
System.out.println();
}
}
}
输出:
3
3
3
[Ljava.lang.String;@4554617c
1 泡泡龙
2 广深小龙
3、Arrany工具类方法(下列只是小部分)
package com.array1219;
/*
@USER PPL-泡泡龙 or 广深小龙
@date 2020-12-20 10:46
*/
import java.util.Arrays;
public class ArrayTest {
public static void main(String[] args) {
int[] arrInt1 = {1, 2, 3, 4, 5};
int[] arrInt2 = {6, 2, 3, 4, 5};
// 判断两个数组,是否相等:equals
boolean isEq = Arrays.equals(arrInt1, arrInt2);
System.out.println(isEq);
// 数组输出为字符串
System.out.println(Arrays.toString(arrInt1));
// 排序,从小到大
Arrays.sort(arrInt2);
System.out.println(Arrays.toString(arrInt2));
// 将值放入数组中
Arrays.fill(arrInt1, 1);
System.out.println(Arrays.toString(arrInt1));
// 二分查找(前提是数组值小到大排序,可先使用sort)
int[] number = {45, 4, 5, 6, 8, 55, 62, 66};
Arrays.sort(number); // 先排序
int index = Arrays.binarySearch(number, 8);
if (index >= 0) {
System.out.println("找到下标为:" + index + " 值为:" + number[index]);
} else {
System.out.println("数组中不存在此数据");
}
}
}
输出:
false
[1, 2, 3, 4, 5]
[2, 3, 4, 5, 6]
[1, 1, 1, 1, 1]
找到下标为:3 值为:8
感谢尚硅谷在B站开源教学视频提供学习,欢迎来大家QQ交流群一起学习:482713805