在一个程序执行的过程中,各条语句的执行顺序对程序的结果是有直接影响的。也就是说程序的流程对运行结果有直接的影响。所以,我们必须清楚每条语句的执行流程。而且,很多时候我们要通过控制语句的执行顺序来实现我们要完成的功能。
流程控制语句分类
顺序结构
选择结构
循环结构
是程序中最简单最基本的流程控制,没有特定的语法结构,按照代码的先后顺序,依次执行,程序中大多数的代码都是这样执行的。 总的来说:写在前面的先执行,写在后面的后执行
public class Cherris { public static void main(String[] args) { System.out.println("开始"); System.out.println("语句A"); System.out.println("语句B"); System.out.println("语句C"); System.out.println("结束"); } }
顺序结构:
开始
语句A
语句B
语句C
结束
if语句有三种格式
if语句第一种格式: if(关系表达式)
{
语句体
}
执行流程
首先判断关系表达式看其结果是true还是false
如果是true就执行语句体
如果是false就不执行语句体
public class Cherris { public static void main(String[] args) { System.out.println("开始"); int a=10; int b=15; if (a>b){ System.out.println("a大于b"); } int c=10; if (a==c){ System.out.println("a等于c"); } System.out.println("结束"); } }
注意事项:
1.关系表达式无论简单还是复杂,结果都是boolean类型。
2.if语句控制的如果是一条语句,大括号可以省略;如果是多条语句就不能省略,建议永远不要省略。
3.一般来说有左大括号就没有分号,有分号就没有左大括号。
if语句第二种格式:if……else
if(关系表达式) {
语句体1;
}else
{
语句体2;
}
执行流程 首先判断关系表达式看其结果是true还是false
如果是true就执行语句体1
如果是false就执行语句体2
public class Cherris { public static void main(String[] args) { System.out.println("开始"); int a=10; int b=15; if (a>b){ System.out.println("a大于b"); }else { System.out.println("a小于c"); } System.out.println("结束"); } }
奇偶数:
从键盘输入一个数判断是奇数还是偶数
import java.util.Scanner; public class Cherris { public static void main(String[] args) { Scanner xc=new Scanner(System.in); System.out.println(); int number = xc.nextInt(); if (number%2==0){ System.out.println("number:"+"偶数"); }else { System.out.println("number:"+"奇数"); } } }
输入月份,确认季节。
import java.util.Scanner; public class Cherris2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int month = sc.nextInt(); if (month<1 | month>12){ System.out.println("输入的数字有误"); }else if (month<=5&month>3){ System.out.println("spring"); }else if (month>5&month<=8){ System.out.println("summer"); }else if (month>8&month<=11){ System.out.println("aultume"); }else if (month>11|month<3){ System.out.println("winter"); } } }
三种方式比大小:if……else if……else if嵌套 三目运算实现
import java.util.Scanner; public class Cherris2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入第一个数据"); int score1 = sc.nextInt(); System.out.println("请输入第二个数据"); int score2 =sc.nextInt(); System.out.println("请输入第三个数据"); int score3 = sc.nextInt(); if(score1>score2){ System.out.println("最大值:"+score1); }else if(score2>score3){ System.out.println("最大值:"+score2); }else { System.out.println("最大值;"+score3); } System.out.println("==============嵌套===================="); if(score1>score2){ if(score1>score3){ System.out.println("max"+score1); }else{ System.out.println("max"+score3); } }else{if (score2>score3){System.out.println("max"+score2);} else {System.out.println("max"+score3);} } System.out.println("===============三目运算实现============="); int a=(score1>score2)?score1:score2; int b=(score3>a)?score3:a; System.out.println("big"+b); } }
import java.util.Scanner; public class Cherris2 { public static void main(String[] args) { //创建键盘录入对象 Scanner sc = new Scanner(System.in); System.out.println("请输入一个数据(1-7)"); int number = sc.nextInt(); switch (number){ default: System.out.println("输入的数据有误"); // break; 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; } } }
利用case的穿透性求月份所在的季节:
import java.util.Scanner; public class Cherris { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入月份"); int month = sc.nextInt(); switch (month){ case 12: case 1: case 2: System.out.println("winter"); break; case 3: case 4: case 5: System.out.println("spring"); break; case 6: case 7: case 8: System.out.println("spring"); break; case 9: case 10: case 11: System.out.println("spring"); break; default: System.out.println("输入的月份数有误!"); break; } } }