第三章:流程控制与数组
3.1 顺序结构
在任何编程语言中最常见的结构就是顺序结构,顺序结构最常见的顺序就是自上而下依次运行,中间没有任何判断和跳转
如果main方法的多行代码直接没有任何的流程控制,则顺序总是自上而下依次运行。
3.2 分支结构
Java提供了两种常见的分支控制结构:if和switch语句。其中if语句布尔表达式或布尔值作为分支条件来进行分支控制;而switch则用于对多个整型值进行分配,从而实现分支控制
3.2.1 if条件语句
if语句有三种形式:
/**
* @author wangquan [email protected]
* @create 2019/3/3 15:23
* @since JDK8u191
*/
public class DemoIfTest {
public static void main(String[] args) {
int i = 2;
int j = 5;
int temp = 7;
//第一种形式
if (temp == i + j){
System.out.println("2 + 5 = 7");
}
//第二种形式
if (temp == i + j){
System.out.println("2 + 5 = 7");
}else{
System.out.println("2 + 5不等于7");
}
//第三种形式,可以有零或多个else if语句,最后的else语句也可以省略
if (temp == i + j){
System.out.println("7 = 2 + 5");
}else if (i == j + temp){
System.out.println("2 = 5 + 7");
}else if (j == i + temp){
System.out.println("5 = 2 + 7");
}
}
}
3.2.2 Java7增强后的switch分支语句
- switch语句由一个控制表达式多个case标签组成,和if语句不同的是,switch语句后面的控制表达式的数据类型只能是byte、short、char、int四种整数类型,枚举类型和java.lang.String类型,不能是boolean类型
switch语句往往需要在case标签后紧跟一个代码块,case标签作为这个代码块的标识。switch语句的语法格式如下:
/**
* switch语句语法格式
* @author wangquan [email protected]
* @create 2019/3/3 15:38
* @since JDK8u191
*/
public class SwitchTest {
public static void main(String[] args) {
int num = 1;
switch (num){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
default:
System.out.println("数据错误");
}
}
}
3.3 循环结构
3.3.1 循环概述
循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环体语句,当反复执行这个循环体时,需要在合适的适合把循环判断条件改为false,从而结束循环,否则循环将一直持续下去,形成死循环。
3.3.2 while循环语句
while循环语句格式如下:
初始化语句
while(条件判断){
循环体;
布进语句;
}
/**
* while循环测试
*
* @author wangquan [email protected]
* @create 2019/3/3 15:51
* @since JDK8u191
*/
public class WhileTest {
public static void main(String[] args) {
//while循环
int i = 1;
while (i <= 5){
System.out.println("HelloWorld");
i ++ ;
}
}
}
3.3.3 do-while循环语句
do-while循环和while循环非常相似,区别在于,do-while不管条件判断是否成立总会先执行一次循环体,再根据判断是否循环执行循环体;代码如下
/**
* do-while循环测试
* @author wangquan [email protected]
* @create 2019/3/3 15:54
* @since JDK8u191
*/
public class DoWhileTest {
public static void main(String[] args) {
int i= 0;
do {
//不管怎样先打印一遍“我错啦”再进行条件判断
System.out.println("我错啦");
i++;
}while (i <= 5);
}
}
3.3.4 for循环语句
for循环使用最普遍代码格式如下:
/**
* for循环测试
*
* @author wangquan [email protected]
* @create 2019/3/3 16:02
* @since JDK8u191
*/
public class ForTest {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println("我错啦");
}
}
}
3.4 控制循环结构
3.4.1 使用break结束循环
使用break结束循环而不是等到条件判断为false时结束。代码如下
/**
* for循环测试
*
* @author wangquan [email protected]
* @create 2019/3/3 16:02
* @since JDK8u191
*/
public class ForTest {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 2){
//程序在i==2时立刻结束,退出循环
break;
}
System.out.println("我错啦");
}
}
}
3.4.2 使用continue忽略本次循环剩下语句
continue循环在碰到continue时会忽略本次循环,跳转至下一条循环。代码格式如下
/**
* for循环测试
* * @author wangquan [email protected]
* @create 2019/3/3 16:02
* @since JDK8u191
*/
public class ForTest {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 2){
continue;
}
System.out.println(i);//运行结果为0,1,3,4
}
}
}
3.4.3 使用return结束方法
- return关键字并不是用于结束循环的,return的功能是结束一个方法,当一个方法执行到下一个return语句时,这个方法将被结束。
- Java程序中大部分循环都被放在方法中执行,一旦在循环体内执行到一个return语句时,return语句就会结束该方法,循环也随之结束。
- 与break不同的是return会直接结束整个方法而不是这层循环
除此之外,return也可作为返回值使用
3.5 数组
3.5.1 数组类型
- 数组的概念:是一种容器,可以同时存放多个数据值。
-
数组的特点
1.数组是一种引用数据类型。
2.数组当中的多个数据,类型必须统一
3.数组的长度在程序运行期间不可改变
数组的动态初始化格式:
数据类型[] 数组名称 = new 数据类型[数组长度]
动态初始化指定长度,之间指定数组当中的数据元素个数
静态初始化指定内容,不指定数据个数多少,而是之间将具体的数据内容指定
public class Array {
public static void main(String[] args) {
int[] arrayA = new int[200];
double[] arrayB = new double[250];
String[] arrayC = new String[10];
}
}
数组的静态初始化格式;
数据类型[] 数组名称 = new 数据类型[]{元素1,元素2,元素3,…}
静态初始化的省略格式:数据类型[] 数组名称 = {元素1,元素2,元素3,…}
public class Array {
public static void main(String[] args) {
//动态初始化
int[] arrayA = new int[200];
double[] arrayB = new double[250];
String[] arrayC = new String[10];
//静态初始化
int[] array01 = new int[]{2,3,5,8};
String[] array02 = {"吃饭","睡觉","打豆豆"};
}
}
3.5.2 访问数组元素
访问数组元素的格式:数组名称[索引值]
3.5.3 给动态初始化数组赋值
int[] arrayA = new int[20];
arrayA[1] = 30;
System.out.println(arrayA[1]);
3.5.4 获取数组的长度
获取数组的长度格式:数组.length
public class Length {
public static void main(String[] args) {
int[] arrayA = new int[3];
int[] arrayB = {1,5,9,52,46,87,35,96,46,52,44,22,87,62,4,20,36};
int len = arrayB.length;
System.out.println("数组的长度为"+len);
}
}
3.5.5 数组的遍历输出
使用for循环获取数组里的每个元素
public class ArrayTraverse {
public static void main(String[] args) {
//定义一个数组
int[] array = {1,5,7,3,9};
//使用for循环打印数组每个元素的值
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
3.5.6 数组元素反转
- 本来的样子[10,15,20,25,30,35]
- 之后的样子[35,30,25,20,15,10]
- 要求不能使用新数组,就用原来的数组
/**
* 数组元素反转
*
* @author wangquan [email protected]
* @create 2019/3/3 16:51
* @since JDK8u191
*/
public class ArrayTest {
public static void main(String[] args) {
int[] array = {10, 15, 20, 25, 30,35};
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
for (int min = 0, max = array.length - 1; min < max || min == max + 1; min++, max--) {
int temp = array[min];
array[min] = array[max];
array[max] = temp;
}
System.out.println("=============");
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
上一篇: Java流程控制(三)
下一篇: Java流程控制与数组(三)