根据编写的顺序,从上到下,依次执行。数组
代码演示oop
// 顺序结构 public class Demo01Sequence { public static void main(String[] args) { //顺序执行,根据编写的顺序,从上到下运行 System.out.println("床前明月光"); System.out.println("疑是地上霜"); System.out.println("举头望明月"); System.out.println("低头思故乡"); } }
if语句第一种形式: ifspa
格式:3d
执行流程图code
代码演示单if语句的使用blog
public class DemoIf { //判断2个变量是否相等 public static void main(String[] args) { // 定义两个变量 int a = 10; int b = 20; //a == b 是关系表达式 if (a == b) { //语句体 System.out.println("a等于b"); } int c = 10; //a == c 是关系表达式 if (a == c) { //语句体 System.out.println("a等于c");//a等于c } System.out.println("其余代码");//其余代码 } }
执行流程图开发
代码演示if.. else语句的使用字符串
// 标准的if-else语句, 判断给定的数据是奇数仍是偶数 public class Demo03IfElse { public static void main(String[] args) { int num = 666; // 若是除以2可以余数为0,说明是偶数 if (num % 2 == 0) { //语句体1 System.out.println("偶数");//偶数 } else { //语句体2 System.out.println("奇数"); } System.out.println("其余代码");//其余代码 } }
执行流程图it
代码演示其基本使用for循环
/* 指定考试成绩,判断学生等级 90-100 优秀 80-89 好70-79 良 60-69 及格 60如下 不及格 */ public class Demo05IfElsePractise { public static void main(String[] args) { int score = 120; if (score >= 90 && score <= 100) { System.out.println("优秀"); } else if (score >= 80 && score < 90) { System.out.println("好"); } else if (score >= 70 && score < 80) { System.out.println("良"); } else if (score >= 60 && score < 70) { System.out.println("及格"); } else if (score >= 0 && score < 60) { System.out.println("不及格"); } else { // 单独处理边界以外的不合理状况 System.out.println("数据错误");//数据错误 } } }
// 题目:使用三元运算符和标准的if-else语句分别实现:取两个数字当中的最大值 public class Demo06Max { public static void main(String[] args) { int a = 105; int b = 20; /* 首先使用三元运算符 int max = a > b ? a : b; */ // 使用今天的if语句 int max; if (a > b) { max = a; } else { max = b; } System.out.println("最大值:" + max);//最大值:105 } }
格式:
执行流程图
代码演示,基本使用
public class Demo07Switch { public static void main(String[] args) { //定义变量,判断是星期几 int weekday = 1; //switch语句实现选择 switch (weekday) { 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; // 最后一个break语句能够省略,可是强烈推荐不要省略 } } }
switch语句使用的注意事项:
格式:
执行流程图
代码演示,基本使用
//循环练习:使用for循环,计算1-100之间的偶数和 public class DemoFor { public static void main(String[] args) { //1.定义一个初始化变量,记录累加求和,初始值为0 int sum = 0; //2.利用for循环获取1‐100之间的数字 for (int i = 1; i <= 100; i++) { //3.判断获取的数组是奇数仍是偶数 if (i % 2 == 0) { //4.若是是偶数就累加求和 sum += i; } } //5.循环结束以后,打印累加结果 System.out.println("sum:" + sum);//sum:2550 } }
执行流程图
代码演示,基本使用
//循环练习:使用while循环,计算1-100之间的偶数和 public class DemoWhile { public static void main(String[] args) { //定义一个初始化变量,记录累加求和,初始值为0 int sum = 0; //定义初始化表达式 int i = 1; //使用while循环让初始化表达式的值变化 while (i < 101) { if (i % 2 == 0) { //累加求和 sum += i; } //步进表达式改变变量的值 i++; } //打印求和的变量 System.out.println("1‐100的偶数和是:" + sum);//1‐100的偶数和是:2550 } }
执行流程图
代码演示,基本使用
/* do-while循环的标准格式: do { 循环体 } while (条件判断); 扩展格式: 初始化语句 do { 循环体 步进语句 } while (条件判断); */ //循环练习:使用do..while循环,计算1-100之间的偶数和 public class Demo11DoWhile { public static void main(String[] args) { //定义一个初始化变量,记录累加求和,初始值为0 int sum = 0; // 初始化语句 int i = 0; do { //判断获取的数组是奇数仍是偶数 if (i % 2 == 0) { //若是是偶数就累加求和 sum += i; } i++; // 4. 步进语句 } while (i <= 100); // 2. 条件判断 //打印求和的变量 System.out.println("1‐100的偶数和是:" + sum);//1‐100的偶数和是:2550 } }
public class LoopDifference { public static void main(String[] args) { for (int i = 1; i < 0; i++) { System.out.println("Hello"); } // System.out.println(i); 这一行是错误写法!由于变量i定义在for循环小括号内,只有for循环本身才能用。 int i = 1; do { System.out.println("World"); i++; } while (i < 0); // 如今已经超出了do-while循环的范围,咱们仍然可使用变量i System.out.println(i); // 2 } }
用法有常见的两种:
关于循环的选择,有一个小建议:
代码演示
public class DemoBreak { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { // 若是但愿从第4次开始,后续全都不要了,就要打断循环 if (i == 4) { // 若是当前是第4次 break; // 那么就打断整个循环 } System.out.println("Hello" + i); } } }
常见用法
代码演示
public class DemoContinue { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i == 4) { // 若是当前是第4层 continue; // 那么跳过当前次循环,立刻开始下一次(第5层) } System.out.println(i + "层到了。"); } } }
代码演示
/* 永远停不下来的循环,叫作死循环。 死循环的标准格式: while (true) { 循环体 } */ public class DemoDeadLoop { public static void main(String[] args) { while (true) { System.out.println("I Love Java!"); } // System.out.println("Hello"); 错误 } }
嵌套循环格式:
代码实现
public class Test { public static void main(String[] args) { //5*8的矩形,打印5行*号,每行8个 // 外循环5次,内循环8次 for (int i = 0; i < 5; i++) { for (int j = 0; j < 8; j++) { //不换行打印星号 System.out.print("*"); }//内循环打印8个星号后,须要一次换行 System.out.println(); } } }