一、顺序结构
从上到下依次执行:
public class OrderDemo {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
System.out.println(3);
}
}
输出的结果为 1 2 3
二、选择结构
1. if 单选择结构
package Test3;
/*
* if单选择结构:
* if(关系表达式){
* 语句体;
* }
*
* 执行流程:
* 1.计算关系表达式的值,看结果是true还是flase
* 2.若是true则执行语句体
* 3.若是flase则不执行语句体
*
*/
public class IfDemo1 {
public static void main(String[] args) {
int a = 10;
int b = 10;
if(a>b){
System.out.println("hello");
}
if(a==b){
System.out.println("hello");
}
}
}
2. if 双选择结构
package Test3;
/*
* if双选择结构:
* if(关系表达式){
* 语句体1;
* }else{
* 语句体2;
* }
*
* 执行流程:
* 1.计算关系表达式的值,看结果是true还是flase
* 2.若是true则执行语句体1
* 3.若是flase则执行语句体2
*
*/
public class IfDemo2 {
public static void main(String[] args) {
int a = 10;
int b = 10;
if(a==b){
System.out.println("hello");
}else{
System.out.println("你好");
}
if(a>b){
System.out.println("hello");
}else{
System.out.println("你好");
}
}
}
3. if 多选择结构
package Test3;
import java.util.Scanner;
/*
* if多选择结构:
* if(关系表达式1){
* 语句体1;
* }else if(关系表达式2){
* 语句体2;
* }else if(关系表达式3){
* 语句体3;
* }else if(关系表达式4){
* 语句体4;
* }else{
* 语句体5;
* }
*
* 执行流程:
* 1.计算关系表达式的值,判断是true还是false
* 2.如果某个关系表达式为true,则执行该表达式下的语句,否则跳过该语句
* 3.一旦一个else if语句检测为true,其它的else if语句以及else语句都将被跳过执行
*
*/
public class ifDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生的成绩:");
int a = scanner.nextInt();
if(a<=100&&a>=90){
System.out.println("学生的成绩等级为A!");
}else if(a<90&&a>=80){
System.out.println("学生的成绩等级为B!");
}else if(a<80&&a>=70){
System.out.println("学生的成绩等级为C!");
}else if(a<70&&a>=60){
System.out.println("学生的成绩等级为D!");
}else{
System.out.println("学生的成绩等级为不及格!");
}
scanner.close();
}
}
4.嵌套的 if 结构
package Test3;
/*
* 嵌套的if结构:
* if(关系表达式1){
* 语句体1;
* if(关系表达式2){
* 语句体2;
* if(关系表达式3){
* 语句体3;
* }
* }
* }
*/
嵌套的 if 结构一般可以简化复杂的操作,在日后的工作中较为常用。
5. Switch多选择结构
import java.util.Scanner;
/*switch语句格式:
1. switch(表达式){
2. case 值1:
3. 语句体1;
4. break;
5. case 值2:
6. 语句体1;
7. break;
8. ......
9. default:
10. 语句体n+1;
11. break;
12. }
13.
14. 执行流程:
15. 1.计算表达式的值;
16. 2.将得到的值依次与case后的值进行匹配,一旦匹配成功则执行相应的语句体,遇到break则结束;
17. 3.若是都不匹配,则执行语句体n+1,遇到break则结束;
*/
public class SwitchDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩等级:");
String a = scanner.nextLine();
switch (a){
case "A":
System.out.println("成绩为优秀!");
break;
case "B":
System.out.println("成绩为良好!");
break;
case "C":
System.out.println("成绩为中等!");
break;
case "D":
System.out.println("成绩为及格!");
break;
default:
System.out.println("成绩为不及格!");
break;
}
scanner.close();
}
}
注意:
1、case 后面的值不允许重复。
2、case 后面的值只能是字面量(常量),不能是变量。
3、switch ( ) 中可以接收的类型:
a、基本数据类型:byte,short,char,int。
b、引用数据类型:jdk5版本开始可以是枚举,jdk7版本开始可以是String字符串。
4、case穿透现象:
public class SwitchDemo02 {
public static void main(String[] args) {
int a = 1;
switch (a){
case 1:
System.out.println("1");
case 2:
System.out.println("2");
break;//只有遇到break才停止
case 3:
System.out.println("3");
break;
default:
System.out.println("4");
break;
}
}
}//输出结果为 1 2
有时可以运用 case 穿透现象简化代码,比如 switch 中出现了重复的代码则可以利用该现象,例如:
import java.util.Scanner;
public class SwitchDemo02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
int week = sc.nextInt();
switch (week){
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("工作日");//1~5都为工作日
break;
case 6:
case 7:
System.out.println("休息日");//6~7都为休息日
break;
default:
System.out.println("您的输入有误!");
}
}
}
jdk14版本开始,case 后面允许编写多个数据,多个数据中间使用逗号分隔,上述代码可简化为:
import java.util.Scanner;
public class SwitchDemo02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
int week = sc.nextInt();
switch (week){
case 1,2,3,4,5:
System.out.println("工作日");
break;
case 6,7:
System.out.println("休息日");
break;
default:
System.out.println("您的输入有误!");
}
}
}
其实上述代码还可简化,但是对jdk版本有要求,所以了解即可:
import java.util.Scanner;
public class SwitchDemo02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
int week = sc.nextInt();
switch (week){
case 1,2,3,4,5 -> System.out.println("工作日");
case 6,7 -> System.out.println("休息日");
default -> System.out.println("您的输入有误!");
}
}
}
三、循环结构
1. While 循环
基本语法:
while(布尔表达式){
//代码语句
}
//只要布尔表达式为true,循环就会一直执行下去
实例:计算1+2+3+…+100的值
public class WhileDemo01 {
public static void main(String[] args) {
//计算1+2+3+......+100的值
int i = 0;
int sum = 0;
while(i<100){
i++;
sum = sum + i;
}
System.out.println(sum);
}
}
2. DoWhile 循环
基本语法:
do{
//代码语句
}while(布尔表达式);
//DoWhile循环与While循环最大的区别就是DoWhile循环至少会执行一次
实例:DoWhile循环与While循环的区别:
public class DoWhileDemo01 {
public static void main(String[] args) {
int a = 0;
while(a<0){
System.out.println(a);
a++;
}
System.out.println("==========");
do{
System.out.println(a);
a++;
}while(a<0);
}
}
运行结果为:
==========
0
3. For 循环
基本语法:
for(初始化;布尔表达式;更新){
//代码语句
}
//For循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构
for( ; ; ){
//代码语句
}
//这是一种死循环,没有任何条件限制,程序会一直执行下去
注意:
1、循环 { } 中定义的变量,在每一轮循环结束后,都会从内存中释放。
2、循环 ( ) 中定义的变量,在整个循环结束后,都会从内存中释放。
所以,下列代码在IDEA中运行是会报错的:
public class ForDemo05 {
public static void main(String[] args) {
for(int i = 1;i <= 5;i++){
int j = 10;
System.out.println(i);
}
System.out.println(i);//报错,因为i在整个循环结束后就释放了内存
System.out.println(j);//报错,因为j在每一轮循环结束后就释放了内存
}
}
3、循环语句 { } 和 ( )之间不要写分号。
下面给出一些 for 循环的实例:
实例1:计算0到100之间的奇数和偶数的和
public class ForDemo01 {
//计算0到100之间的奇数和偶数的和
public static void main(String[] args) {
int oddSum = 0;
int evenSum = 0;
for(int i=0;i<=100;i++){
if(i%2==0){
evenSum += i;
}
else{
oddSum += i;
}
}
System.out.println("0到100之间奇数的和为:"+oddSum);
System.out.println("0到100之间偶数的和为:"+evenSum);
}
}
运行结果为:
0到100之间奇数的和为:2500
0到100之间偶数的和为:2550
实例2:用while或者for循环输出1-1000之间能被5整除的数,并且每行输出3个
public class ForDemo02 {
//用while或者for循环输出1-1000之间能被5整除的数,并且每行输出3个
public static void main(String[] args) {
int n=0;
for(int i=1;i<=1000;i++){
if(i%5==0){
System.out.print(i+"\t");
n++;
if(n%3==0){
System.out.print("\n");
//实现换行也可以用System.out.println();
}
}
}
//println 输出会换行
//print 输出不会换行
}
}
循环嵌套
循环嵌套:在循环语句中,继续出现循环语句。
下面给出一些具体的实例:
实例1:在控制台使用 * 打印4行5列的矩形
public class ForDemo06 {
public static void main(String[] args) {
//控制行
for (int i = 1; i <= 4; i++){
//控制列
for(int j = 1; j <= 5; j++){
System.out.print("*");//System.out.print()打印完成后不换行
}
System.out.println();//打印完成后换行
}
}
}
运行结果为:
*****
*****
*****
*****
实例2:在控制台使用 * 打印5行的直角三角形
public class ForDemo06 {
public static void main(String[] args) {
//控制行
for (int i = 1; i <= 5; i++){
//控制列
for(int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
运行结果为:
*
**
***
****
*****
实例3:在控制台使用 * 打印5行的等腰三角形
可以将等腰三角形分解为箭头所指的三个部分来操作
public class TestDemo {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
//打印第一个箭头所指部分的空白直角三角形
for(int j = 5; j >= i; j--){
System.out.print(" ");
}
//打印第二个箭头所指部分的带*的直角三角形,这与空白直角三角形正好相反
for(int j = 1; j <= i; j++){
System.out.print("*");
}
//打印第三个箭头所指部分的带*的直角三角形
for(int j = 1; j < i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
运行结果为:
*
***
*****
*******
*********
实例4:在控制台打印九九乘法表
public class ForDemo03 {
public static void main(String[] args) {
//打印九九乘法表
for(int i = 1; i <= 9; i++){
for(int j = 1; j <= i; j++){
System.out.print(j + "*" + i + "=" + j * i + "\t");//(\t)为制表符
}
System.out.println();
}
}
}
运行结果为:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
4.增强 For 循环
基本语法:
for(声明语句 : 表达式){
//代码语句
}
//Java5引入的一种主要用于数组或者集合的增强型For循环
实例:
public class ForDemo04 {
public static void main(String[] args) {
int[] numbers={10,20,30,40,50};
//普通for循环
for(int i = 0;i<5;i++){
System.out.println(numbers[i]);
}
System.out.println("====================");
//增强for循环
for(int x:numbers){
System.out.println(x);
}
}
}
四、跳转控制语句
1. break
break 可以结束循环,也可以结束switch语句。
注意:break 只能在循环或者是 switch 语句中使用。
例如:
For(int i = 0; i<=5; i++){
if(i == 3){
break;
}
System.out.println("The number is:"+i);
}
运行结果为:
The number is:0
The number is:1
The number is:2
2. continue
continue 可以跳出本次循环,继续执行下次循环。
注意:continue 只能在循环中使用。
例如:
For(int i = 0;i<=5;i++){
if(i = =3){
continue;
}
System.out.println("The number is:"+i);
}
运行结果为:
The number is:0
The number is:1
The number is:2
The number is:4
The number is:5
3.特殊情况
可以给循环取名,通过 (break 循环名) 来跳出该循环。
以学生管理系统的菜单搭建的实例来说明该情况:
import java.util.Scanner;
public class TestDemo02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//可以给循环取名
lo:while (true){//while(true)自身是死循环
System.out.println("请输入您的选择:1. 添加学生 2. 删除学生 3. 修改学生 4. 查看学生 5. 退出");
int choice = sc.nextInt();
switch(choice){
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 lo;//此处就跳出while循环
default:
System.out.println("您的输入有误!");
break;
}
}
}
}