目标
程序按结构可以分三种类型:
- 顺序结构
- 分支结构
-
1. 分支结构
1.1 if 语句

if (条件语句){执行语句;……}
条件语句是一个
boolean表达式,返回值为boolean类型 eg: a >100
代码示例public class IfDemo01 {public static void main(String[] args) {int x = 5;if (x < 10) {x++;}System.out.println("x=" + x);}}
1.2 if else 语句

语法结构if (判断条件) {执行语句1……} else {执行语句2……}
代码示例
/**判断数字是奇数还是偶数*/public class IfElseDemo {public static void main(String[] args) {int num = 19;if (num % 2 == 0) {// 判断条件成立,num被2整除System.out.println("num是一个偶数");} else {System.out.println("num是一个奇数");}}}
1.3 if else if 语句

语法结构if (判断条件1) {执行语句1}else if (判断条件2) {执行语句2}// 此处还可以有多个 else if 语句else if (判断条件n) {执行语句n}// 也可以没有 else 语句else {执行语句n+1}
代码示例
public class IfElesIfDemo {public static void main(String[] args) {int score = 75; // 定义学生成绩// 满足条件 score > 80if (score > 80) {System.out.println("该成绩的等级为优");}// 不满足条件 score > 80 ,但满足条件 score > 70// score<=80 并且 score >70else if (score > 70) {System.out.println("该成绩的等级为良");}// 不满足条件 score > 70 ,但满足条件 score > 60// score<=70 并且 score>60else if (score > 60) {System.out.println("该成绩的等级为中");}// 不满足条件 score > 60// score <=60else {System.out.println("该成绩的等级为差");}}}
1.4 switch 语句

语法结构switch (某种数据类型的数据){case 目标值1:执行语句1break; // break表示跳出switch语句case 目标值2:执行语句2break;......case 目标值n:执行语句nbreak;default: // 之前的case都不满足执行defalut执行语句n+1break;}
注意:
使用
break关键字可以跳出switch语句- 在
switch语句中的表达式只能是byte、short、int,char、String,枚举 - 在JDK5.0中引入的新特性枚举可以作为
switch语句表达式的值 - 在JDK7.0中引入了新特性
String可以作为switch语句表达式的值
代码示例:
public class SwitchDemo01 {public static void main(String[] args) {int week = 5;switch (week) {case 1:System.out.println("星期一");break;case 2:System.out.println("星期二");break;case 3:System.out.println("星期三");break;case 4:System.out.println("星期四");break;case 5:System.out.println("星期五");break;case 6:System.out.println("星期六");break;case 7:System.out.println("星期天");break;default:System.out.println("输入的数字不正确...");break;}}}
在switch语句中,如果case的后面不写break,将出现穿透现象,也就是不会在判断下一个case的值,直接向后运行,直到遇到break,或者整体switch结束。
public class SwitchDemo02 {public static void main(String[] args) {int week = 2;switch (week) {case 1:case 2:case 3:case 4:case 5:// 当 week 满足值 1、2、3、4、5 中任意一个时,处理方式相同System.out.println("今天是工作日");break;case 6:case 7:// 当 week 满足值 6、7 中任意一个时,处理方式相同System.out.println("今天是休息日");break;}}}
2. 循环结构
2.1 while循环

语法结构
while(循环条件){执行语句;}
代码示例
public class WhileDemo {public static void main(String[] args) {int x = 1; // 定义变量x,初始值为1while (x <= 4) { // 循环条件System.out.println("x = " + x); // 条件成立,打印x的值x++; // x进行自增}}}
2.2 do while 循环

语法结构
do {执行语句;} while(循环条件);
代码示例
public class DoWhileDemo {public static void main(String[] args) {int x = 1; // 定义变量x,初始值为1do {System.out.println("x = " + x); // 打印x的值x++; // 将x的值自增} while (x <= 4); // 循环条件}}
while 和 do while 的区别:while循环先判断是否满足循环条件,满足执行while后面{}中的代码,不满足,跳出循环。do while是先执行一次do{}号中的代码,然后判断循环条件,满足条件执行do{}中的代码,不满足跳出循环。
2.3 for 循环

语法结构
for(初始化表达式; 循环条件; 操作表达式){执行语句;}
语法说明
for( 1 ; 2 ; 3){
4
}
第一步,执行 1
第二步,执行 2,如果判断结果为true,执行第三步,如果判断结果为false,执行第五步
第三步,执行 4
第四步,执行 3,然后重复执行第二步
第五步,退出循环
代码示例
public class ForDemo01 {
public static void main(String[] args) {
int sum = 0; // 定义变量sum,用于记住累加的和
for (int i = 1; i <= 4; i++) { // i的值会在1~4之间变化
sum += i; // 实现sum与i的累加
}
System.out.println("sum = " + sum); // 打印累加的和
}
}
2.4 无限循环
while(true){
}
// 或
for(;;){
}
无限循环存在的原因是并不知道循环多少次,而是根据某些条件,来控制是否终止循环
2.5 跳转语句
break 语句
跳出当前循环
public class BreakDemo {
public static void main(String[] args) {
int x = 0; // 定义变量x,初始值为1
while (x <= 4) { // 循环条件
x++; // x进行自增
if (x == 3) {
break;
}
System.out.println("x = " + x); // 打印x的值
}
}
}
continue 语句
继续执行下次循环
public class BreakDemo {
public static void main(String[] args) {
int x = 0; // 定义变量x,初始值为1
while (x <= 4) { // 循环条件
x++; // x进行自增
if (x == 3) {
continue;
}
System.out.println("x = " + x); // 打印x的值
}
}
}
2.6 循环嵌套
语法格式
for(初始化表达式; 循环条件; 操作表达式) {
执行语句;
for(初始化表达式; 循环条件; 操作表达式) {
执行语句;
}
执行语句;
}
代码示例
打印三角形
public class ForForDemo {
public static void main(String[] args) {
int i, j; // 定义两个循环变量
for (i = 1; i <= 9; i++) { // 外层循环打印行
for (j = 1; j <= i; j++) { // 内层循环打印列
System.out.print("* "); // 打印*
}
System.out.print("\n"); // 换行
}
}
}
3. 键盘输入
目标
- 熟悉 Java 帮助文档(API)的使用
- 熟悉使用键盘数据 API
回顾:数据类型分类
- 基本数据类型(4类8种)
- 整数
- short
- byte
- int
- long
- 浮点数
- float
- double
- 字符类型
- char
- 布尔类型
- boolean
- 整数
引用数据类型
导入包
import java.util.Scanner;创建对象实例
Scanner sc = new Scanner(System.in);调用方法
String s = sc.next(); // 用来接收键盘录入的字符串 String s = sc.nextLine(); // 用来接收键盘录入的一行数据 int i = sc.nextInt(); // 用来接收键盘录入的整数ScannerDemo01.java ```java import java.util.Scanner; // 导入包
public class ScannerDemo01 { public static void main(String[] args) { //创建Scanner引用类型的变量 Scanner sc = new Scanner(System.in); //接收键盘输入的数字 System.out.println(“请输入一个数字:”); int n = sc.nextInt(); System.out.println(“n的值为:” + n); //接收键盘输入的字符串 System.out.println(“请输入一个字符串:”); String str = sc.next(); System.out.println(“str的值为:” + str); } } ```
常用方法
| No. | 方法 | 类型 | 描述 |
|---|---|---|---|
| 1 | public Scanner(InputStream source) | 构造 | 接收 InputStream 输入流对象,此为输入源 |
| 2 | public String next() | 普通 | 取出输入数据,以 String 形式返回 |
| 3 | public String nextLine() | 普通 | 取出输入数据,以 String 形式返回 |
| 3 | public 数据类型 nextXxx() | 普通 | 取出指定数据类型的数据 |
**next()**与**nextLine()**区别:**next**:不能得到带有空格的字符串。**nextLine**:以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符;
