顺序控制

前面定义了变量 后面才可以引用,向前引用 不是向后
分支结构
让程序有选择的的执行,分支控制有三种
1) 单分支 if
2) 双分支 if-else
3) 多分支 if-else if -….-else
单分支 if
//if的快速入门import java.util.Scanner;//导入public class If01 {//编写一个main方法public static void main(String[] args) {//编写一个程序,可以输入人的年龄,如果该同志的年龄大于18岁,//则输出 "你年龄大于18,要对自己的行为负责,送入监狱"////思路分析//1. 接收输入的年龄, 应该定义一个Scanner 对象//2. 把年龄保存到一个变量 int age//3. 使用 if 判断,输出对应信息//应该定义一个Scanner 对象Scanner myScanner = new Scanner(System.in);System.out.println("请输入年龄");//把年龄保存到一个变量 int ageint age = myScanner.nextInt();//使用 if 判断,输出对应信息if(age > 18) {System.out.println("你年龄大于18,要对自己的行为负责,送入监狱");}System.out.println("程序继续...");//这一句总会执行}}
双分支结构 if else
//if-else的快速入门import java.util.Scanner;//导入public class If02 {//编写一个main方法public static void main(String[] args) {//编写一个程序,可以输入人的年龄,如果该同志的年龄大于18岁,//则输出 "你年龄大于18,要对//自己的行为负责, 送入监狱"。否则 ,输出"你的年龄不大这次放过你了."////思路分析//1. 接收输入的年龄, 应该定义一个Scanner 对象//2. 把年龄保存到一个变量 int age//3. 使用 if-else 判断,输出对应信息//应该定义一个Scanner 对象Scanner myScanner = new Scanner(System.in);System.out.println("请输入年龄");//把年龄保存到一个变量 int ageint age = myScanner.nextInt();//使用 if-else 判断,输出对应信息if(age > 18) {System.out.println("你年龄大于18,要对自己的行为负责,送入监狱");} else {//双分支System.out.println("你的年龄不大这次放过你了");}System.out.println("程序继续...");}}

上面会输出 韩顺平教育
因为根据判断x>5成立 所以为真进入内层循环,外层的else “x is” + x不会执行
内层判断y>5不成立所以 x+y 不会输出 因为还在内层 会继续往下走 会输出 韩顺平教育
//单分支和双分支的练习//public class IfExercise01 {//编写一个main方法public static void main(String[] args) {//编写程序,声明2个double型变量并赋值。//判断第一个数大于10.0,且第2个数小于20.0,打印两数之和//思路分析double d1 = 33.5;double d2 = 2.6;if(d1 > 10.0 && d2 < 20.0) {System.out.println("两个数和=" + (d1 + d2));}//【课后自己练】定义两个变量int,判断二者的和,//是否能被3又能被5整除,打印提示信息////思路分析//1. 定义两个变量int num1, num2//2. 定义一个变量 int sum = num1 + num2;//3. sum % 3 , 5 后 等于0 说明可以整除//4. 使用 if - else 来提示对应信息//走代码int num1 = 10;int num2 = 1;int sum = num1 + num2;if(sum % 3 == 0 && sum % 5 == 0) {System.out.println("和可以被3又能被5整除");} else {System.out.println("和不能被3和5整除..");}//判断一个年份是否是闰年,闰年的条件是符合下面二者之一://(1)年份能被4整除,但不能被100整除;(2)能被400整除////思路分析//1. 定义 int year 保存年//2. 年份能被4整除,但不能被100整除,// => year % 4 == 0 && year % 100 != 0//3. 能被400整除 => year % 400 == 0//4. 上面的 2 和 3 是 或的关系//代码实现int year = 2028;if( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ) {System.out.println(year + " 是 闰年");} else {System.out.println(year + " 不是 闰年");}}}
多分支结构
多分支演示
1) 信用分为100分时,输出 信用极好;
2) 信用分为(80,99]时,输出 信用优秀; ( ] 前开后闭合 证明大于80 小于99但包含99
3) 信用分为[60,80]时,输出 信用一般;
4) 其它情况 ,输出 信用 不及格
请从键盘输入保国的芝麻信用分,并加以判断
//课堂练习import java.util.Scanner;public class If03 {//编写一个main方法public static void main(String[] args) {/*输入保国同志的芝麻信用分:如果:信用分为100分时,输出 信用极好;信用分为(80,99]时,输出 信用优秀;信用分为[60,80]时,输出 信用一般;其它情况 ,输出 信用 不及格请从键盘输入保国的芝麻信用分,并加以判断假定信用分数为int*/Scanner myScanner = new Scanner(System.in);//接收用户输入System.out.println("请输入信用分(1-100):");//请思考:如果小伙伴输入的不是整数,而是hello..//==>这里我们后面可以使用异常处理机制搞定-》老师点一下int grade = myScanner.nextInt();//先对输入的信用分,进行一个范围的有效判断 1-100, 否则提示输入错误if(grade >=1 && grade <= 100) {//因为有4种情况,所以使用多分支if(grade == 100) {System.out.println("信用极好");} else if (grade > 80 && grade <= 99) { //信用分为(80,99]时,输出 信用优秀;System.out.println("信用优秀");} else if (grade >= 60 && grade <= 80) {//信用分为[60,80]时,输出 信用一般System.out.println("信用一般");} else {//其它情况 ,输出 信用 不及格System.out.println("信用不及格");}} else {System.out.println("信用分需要在1-100,请重新输入:)");}}}

嵌套分支结构
不建议超过三层
案例
参加歌手比赛,如果初赛成绩大于 8.0 进入决赛,否则提示淘汰。并且根据性别提示进入男子组或女子组。【可以让学员先练习下】, 输入成绩和性别,进行判断和输出信息
//课堂练习import java.util.Scanner;public class If03 {//编写一个main方法public static void main(String[] args) {/*输入保国同志的芝麻信用分:如果:信用分为100分时,输出 信用极好;信用分为(80,99]时,输出 信用优秀;信用分为[60,80]时,输出 信用一般;其它情况 ,输出 信用 不及格请从键盘输入保国的芝麻信用分,并加以判断假定信用分数为int*/Scanner myScanner = new Scanner(System.in);//接收用户输入System.out.println("请输入信用分(1-100):");//请思考:如果小伙伴输入的不是整数,而是hello..//==>这里我们后面可以使用异常处理机制搞定-》老师点一下int grade = myScanner.nextInt();//先对输入的信用分,进行一个范围的有效判断 1-100, 否则提示输入错误if(grade >=1 && grade <= 100) {//因为有4种情况,所以使用多分支if(grade == 100) {System.out.println("信用极好");} else if (grade > 80 && grade <= 99) { //信用分为(80,99]时,输出 信用优秀;System.out.println("信用优秀");} else if (grade >= 60 && grade <= 80) {//信用分为[60,80]时,输出 信用一般System.out.println("信用一般");} else {//其它情况 ,输出 信用 不及格System.out.println("信用不及格");}} else {System.out.println("信用分需要在1-100,请重新输入:)");}}}

import java.util.Scanner;public class test {public static void main(String[] args){Scanner myScanner = new Scanner(System.in);System.out.println("请输入当前月份:");int months = myScanner.nextInt();System.out.println("请输入客户年龄:");int age = myScanner.nextInt();if(months > 4 && months < 10){if (age >18 && age < 60){System.out.println("旺季销售 票价60元");}else if(age < 18){System.out.println("旺季销售 儿童票半价 30元");}else if(age > 60){System.out.println("旺季销售 老年票20元");}}else{if(age > 18){System.out.println("淡季销售 票价40");}else{System.out.println("淡季销售 票价20");}}}}
switch结构

请编写一个程序,该程序可以接收一个字符,比如:a,b,c,d,e,f,g
a 表示星期一,b 表示星期二 …
根据用户的输入显示相应的信息.要求使用 switch 语句完成
穿透: 如果表达式等于常量1 执行完常量1下的语句块 如果常量1 语句块下没有break 会直接执行常量2的语句块而不会判断常量2是否符合标准 常量2没有break 继续穿透
穿透是从匹配到的语句往后穿透 前面没有匹配到的不会执行
如果所有的语句都没有break 则都会执行 default也会执行
import java.util.Scanner;public class Switch01 {//编写一个main方法public static void main(String[] args) {/*案例:Switch01.java请编写一个程序,该程序可以接收一个字符,比如:a,b,c,d,e,f,ga表示星期一,b表示星期二 …根据用户的输入显示相应的信息.要求使用 switch 语句完成思路分析1. 接收一个字符 , 创建Scanner对象2. 使用switch 来完成匹配,并输出对应信息代码*/Scanner myScanner = new Scanner(System.in);System.out.println("请输入一个字符(a-g)");char c1 = myScanner.next().charAt(0);////在java中,只要是有值返回,就是一个表达式switch(c1) {case 'a' : //这里是冒号System.out.println("今天星期一,猴子穿新衣");break;case 'b' :System.out.println("今天星期二,猴子当小二");break;case 'c' :System.out.println("今天星期三,猴子爬雪山..");break;//.....default:System.out.println("你输入的字符不正确,没有匹配的");// default后面可以没有 break 一样执行完退出循环}System.out.println("退出了switch ,继续执行程序");}}
switch细节

public class SwitchDetail {//编写一个main方法public static void main(String[] args) {//细节1//表达式数据类型,应和case 后的常量类型一致,//或者是可以自动转成可以相互比较的类型,比如输入的是字符,而常量是 int//细节2//switch(表达式)中表达式的返回值必须是://(byte,short,int,char,enum[枚举],String) 新版本好像可以string字符串?//细节3//case子句中的值必须是常量(1,'a')或者是常量表达式,而不能是变量////细节4//default子句是可选的,当没有匹配的case时,执行default//如果没有default 子句,有没有匹配任何常量,则没有输出////细节5//break语句用来在执行完一个case分支后使程序跳出switch语句块;//如果没有写break,程序会顺序执行到switch结尾,除非执行到breakchar c = 'b';char c2 = 'c';switch(c) {case 'a' : // case 20 不会报错 char 于int之间可以相互转换System.out.println("ok1");break;case 'b' : //如果这里写 case 'hello' 编译会报错 char>string报错System.out.println("ok2");break;case c2 : //报错 这里不允许是变量名字System.out.println("ok2");break;default :System.out.println("ok3");}System.out.println("退出了switch,继续执行..");}}
import java.util.Scanner;public class SwitchExercise {//编写一个main方法public static void main(String[] args) {//使用 switch 把小写类型的//char型转为大写(键盘输入)。只转换 a->A, b->B, c, d, e.//其它的输出 "other"。//创建Scanner对象Scanner myScanner = new Scanner(System.in);System.out.println("请输入a-e");char c1 = myScanner.next().charAt(0);switch(c1) {case 'a' :System.out.println("A");break;case 'b' :System.out.println("B");break;case 'c' :System.out.println("C");break;case 'd' :System.out.println("D");break;case 'e' :System.out.println("E");break;default :System.out.println("你的输入有误~");}}}
public class SwitchExercise {//编写一个main方法public static void main(String[] args) {//对学生成绩大于60分的,输出"合格"。低于60分的,//输出"不合格"。(注:输入的成绩不能大于100), 提示 成绩/60//思路分析// 结果只有两种 一个是合格 一个是不合格 所以需要把用户输入转换// 整型相除 没有余数和小数// 如果成绩在 [60,100] , (int)(成绩/60) = 1// 如果成绩在 [0,60) , (int)(成绩/60) = 0//代码实现double score = 1.1;//使用if-else 保证输入的成绩有有效的 0-100//看了当老师的分析和代码演示后,自己一定要独立完成(不看老韩代码,也能写)if( score >= 0 && score <= 100) {switch ((int)(score / 60)) {case 0 :System.out.println("不合格");break;case 1 :System.out.println("合格");break;// default :// System.out.println("输入有误");}} else {System.out.println("输入的成绩在0-100");}}}}
import java.util.Scanner;public class SwitchExercise {//编写一个main方法public static void main(String[] args) {//根据用于指定月份,//打印该月份所属的季节。//3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季//[课堂练习, 提示 使用穿透 ]////思路分析//1. 创建Scanner对象, 接收用户输入//2. 使用 int month 接收//3. 使用switch 来匹配 ,使用穿透来完成,比较简洁//4. 因为 3 4 5月是一个结果 所以3 4 5可以穿透 其它季节也是一样Scanner myScanner = new Scanner(System.in);System.out.println("输入月份");int month = myScanner.nextInt();switch(month) {case 3:case 4:case 5:System.out.println("这是春季");break;case 6:case 7:case 8:System.out.println("这是夏季");break;case 9:case 10:case 11:System.out.println("这是秋季");break;case 1:case 2:case 12:System.out.println("这是冬季");break;default :System.out.println("你输入的月份不对(1-12)");}}}
switch和if的使用场景
1) 如果判断的具体数值不多,而且符合 byte、 short 、int、 char, enum[枚举], String 这6 种类型。虽然两个语句都可以使用,建议使用 swtich 语句。
2) 其他情况:对区间判断,对结果为 boolean 类型判断,使用 if,if 的使用范围更广
for 循环控制
让你的代码可以循环的执行
举例
打印 10 句 “你好,韩顺平教育!”。
没有循环的方法
System.out.println(“你好,韩顺平教育!”);
System.out.println(“你好,韩顺平教育!”);
…
写十行
缺点
- 代码很冗余 重复的代码大量出现
- 修改很麻烦,如果要输出1000行,再改为500行,在改10000行,修改工作量会很大。 ```java
public class For01 {
//编写一个main方法public static void main(String[] args) {//打印10句 "你好,韩顺平教育!"//传统的方法// System.out.println("你好,韩顺平教育");// System.out.println("你好,韩顺平教育");// System.out.println("你好,韩顺平教育");// System.out.println("你好,韩顺平教育");// System.out.println("你好,韩顺平教育");// System.out.println("你好,韩顺平教育");// System.out.println("你好,韩顺平教育");// System.out.println("你好,韩顺平教育");// System.out.println("你好,韩顺平教育");// System.out.println("你好,韩顺平教育");//// 使用for循环控制for( int i = 1; i <= 10; i++) { // 1(int i = 1; 变量初始化)2( i <= 10; 循环条件) 4(i++ 循环变量迭代)System.out.println("你好,韩顺平教育" + i); //3(System.out.println("你好,韩顺平教育" + i); 循环操作)}}
}
1. for 关键字,表示循环控制1. for 有四要素: (1)循环变量初始化(2)循环条件(3)循环操作(4)循环变量迭代1. 循环操作 , 这里可以有多条语句,也就是我们要循环执行的代码1. 如果 循环操作(语句) 只有一条语句,可以省略 {}, 建议不要省略<br /><a name="OudMq"></a>### for循环细节1) 循环条件是返回一个布尔值的表达式<br />2) for(;循环判断条件;) 中的初始化和变量迭代可以写到其它地方,但是两边的分号不能省略。3) 循环初始值可以有多条初始化语句,但要求类型一样,并且中间用逗号隔开,循环变量迭代也可以有多条变量迭代语句,中间用逗号隔开。<br />4) 使用内存分析法,老师分析输出下面代码输出什么?1)```java//演示for的使用细节public class ForDetail {//编写一个main方法public static void main(String[] args) {//for(;循环判断条件;)//中的初始化和变量迭代可以写到其它地方,但是两边的分号不能省略// 使用for循环控制int i = 1; //循环变量初始化 可以写到全局 一般用于迭代的对象需要在全局使用这样的情况。for( ; i <= 10 ; ) {System.out.println("hello,韩顺平教育" + i);i++; //循环变量迭代 可以写到这里}System.out.println("i=" + i);//11 ok// int j = 1;// //补充// for(;;) { //表示一个无限循环,死循环// System.out.println("ok~" + (j++));// }}}
//演示for的使用细节public class ForDetail {//编写一个main方法public static void main(String[] args) {//循环初始值可以有多条初始化语句,但要求类型一样,并且中间用逗号隔开,//循环变量迭代也可以有多条变量迭代语句,中间用逗号隔开// 分析代码输出什么int count = 3;for (int i = 0,j = 0; i < count; i++, j += 2) {System.out.println("i=" + i + " j=" + j);}}}
编程思想解析
1. 化繁为简 : 即将复杂的需求,拆解成简单的需求,逐步完成
2. 先死后活 : 先考虑固定的值,然后转成可以灵活变化的值
举例
打印1~100之间所有是9的倍数的整数,统计个数 及 总和
思路过程
化繁为简
(1) 先完成 输出 1-100的值
public class ForExercise {//编写一个main方法public static void main(String[] args) {//(1) 完成 输出 1-100的值for(int i = 1; i <= 100; i++) {System.out.println("i=" + i);}}}
(2) 对输出结果,进行过滤,只输出9的倍数 i % 9 ==0
public class ForExercise {//编写一个main方法public static void main(String[] args) {//(1) 完成 输出 1-100的值for(int i = 1; i <= 100; i++) {if (i % 9 == 0){System.out.println("i=" + i);}}}}
(3) 统计个数 定义一个变量 int count = 0; 当 条件满足时 count++;
public class fordetail {//编写一个main方法public static void main(String[] args) {//(1) 完成 输出 1-100的值int count = 0;for(int i = 1; i <= 100; i++) {if (i % 9 == 0){System.out.println("i=" + i);count++;}}System.out.println("count=" + count);}}
4)总和 , 定义一个变量 int sum = 0; 当条件满足时累积 sum += i;
public class ForExercise {//编写一个main方法public static void main(String[] args) {//(1) 完成 输出 1-100的值int count = 0;int sum = 0;for(int i = 1; i <= 100; i++) {if (i % 9 == 0){System.out.println("i=" + i);count++;sum +=i;}}System.out.println("count=" + count);System.out.println("sum=" + sum);}}
先死后活
(1) 为了适应更好的需求,把范围的开始的值和结束的值,做出变量
public class ForExercise {//编写一个main方法public static void main(String[] args) {//(1) 完成 输出 1-100的值int count = 0;int sum = 0;int start = 10; //增加开始结束变量int end = 500;for(int i = start; i <= end; i++) { //修改表达式中的开始 从固定值改为变量if (i % 9 == 0){System.out.println("i=" + i);count++;sum +=i;}}System.out.println("count=" + count);System.out.println("sum=" + sum);}}
(2) 还可以更进一步 9 倍数也做成变量 int t = 9;
public class ForExercise {//编写一个main方法public static void main(String[] args) {//打印1~100之间所有是9的倍数的整数,统计个数 及 总和.[化繁为简,先死后活]//老韩的两个编程思想(技巧)//1. 化繁为简 : 即将复杂的需求,拆解成简单的需求,逐步完成//2. 先死后活 : 先考虑固定的值,然后转成可以灵活变化的值////思路分析//打印1~100之间所有是9的倍数的整数,统计个数 及 总和//化繁为简//(1) 完成 输出 1-100的值//(2) 在输出的过程中,进行过滤,只输出9的倍数 i % 9 ==0//(3) 统计个数 定义一个变量 int count = 0; 当 条件满足时 count++;//(4) 总和 , 定义一个变量 int sum = 0; 当条件满足时累积 sum += i;//先死后活//(1) 为了适应更好的需求,把范围的开始的值和结束的值,做出变量//(2) 还可以更进一步 9 倍数也做成变量 int t = 9;int count = 0; //统计9的倍数个数 变量int sum = 0; //总和int start = 10;int end = 200;int t = 5; // 倍数for(int i = start; i <= end; i++) {if( i % t == 0) { //把这里的固定值改为变量值System.out.println("i=" + i);count++;sum += i;//累积}}System.out.println("count=" + count);System.out.println("sum=" + sum);}}
完成下面的表达式输出 
化繁为简
先输出0-5
public class ForExercise02{//编写一个main方法public static void main(String[] args) {//化繁为简for( int i = 0; i <= 5; i++) {System.out.println(i );}}}
根据图片 i + (5-i) = 5 前面的计算公式可以写成
public class ForExercise02 {//编写一个main方法public static void main(String[] args) {//化繁为简for( int i = 0; i <= 5; i++) {System.out.println(i + "+" + (5-i) + "=" + 5);}}}
由死变活 把固定值改为变量
public class ForExercise02 {//编写一个main方法public static void main(String[] args) {//化繁为简//先死后活int n = 9;for( int i = 0; i <= n; i++) {System.out.println(i + "+" + (n-i) + "=" + n);}}}
while循环


//while循环的案例//public class While01 {//编写一个main方法public static void main(String[] args) {//输出10句 你好,韩顺平教育int i = 1; //循环变量初始化while( i <= 10 ) {//循环条件System.out.println("你好,韩顺平教育" + i);//执行语句i++;//循环变量迭代}System.out.println("退出while ,继续.." + i); // 11}}
public class WhileExercise {//编写一个main方法public static void main(String[] args) {// 打印1―100之间所有能被3整除的数 [使用while, 老师评讲 ]// 化繁为简, 先死后活int i = 1;int endNum = 100;while( i <= endNum) {if( i % 3 == 0) {System.out.println("i=" + i);}i++;//变量自增 自增是while循环自增 所以写在外面。}// 打印40―200之间所有的偶数 [使用while, 课后练习]// 化繁为简, 先死后活(利于思考)//System.out.println("========");int j = 40; //变量初始化while ( j <= 200) {//判断if( j % 2 == 0) {System.out.println("j=" + j);}j++;//循环变量的迭代}}}
do..while循坏控制
循环变量初始化; do{
循环体(语句);循环变量迭代;
}while(循环条件);
do while至少先执行一次。

public class DoWhile01 {//编写一个main方法public static void main(String[] args) {//输出10句 你好,韩顺平教育int i = 1; //循环变量初始化do {//循环执行语句System.out.println("你好,韩顺平教育" + i);//循环变量迭代i++;}while(i <= 10);System.out.println("退出 do..while 继续执行.." + i);//11}}
public class DoWhileExercise01 {//编写一个main方法public static void main(String[] args) {//统计1---200之间能被5整除但不能被3整除的 个数//化繁为简//(1) 使用do-while输出 1-200//(2) 过滤 能被5整除但不能被3整除的数 %//(3) 统计满足条件的个数 int count = 0;//先死后活//(1) 范围的值 1-200 你可以做出变量//(2) 能被5整除但不能被3整除的 , 5 和 3 可以改成变量int i = 1;int count = 0; //统计满足条件的个数do {if( i % 5 == 0 && i % 3 != 0 ) {System.out.println("i=" + i);count++;}i++;}while(i <= 200);System.out.println("count=" + count);}}
import java.util.Scanner;public class DoWhileExercise02 {//编写一个main方法public static void main(String[] args) {//如果李三不还钱,则老韩将一直使出五连鞭,直到李三说还钱为//[System.out.println("老韩问:还钱吗?y/n")] do...while ..////化繁为简//(1) 不停的问还钱吗?//(2) 使用char answer 接收回答, 定义一个Scanner对象//(3) 在do-while 的while 判断如果是 y 就不在循环//一定自己动脑筋..Scanner myScanner = new Scanner(System.in);char answer = ' ';do {System.out.println("老韩使出五连鞭~");System.out.println("老韩问:还钱吗?y/n");answer = myScanner.next().charAt(0);System.out.println("他的回答是" + answer);}while(answer != 'y');//判断条件很关键 这个的判断条件是必须是布尔值类型 true false 这里是继续循环需要满足的条件System.out.println("李三还钱了");}}
多重循环控制 难点
1) 将一个循环放在另一个循环体内,就形成了嵌套循环。其中,for ,while ,do…while 均可以作为外层循环和内层循环。
【建议一般使用两层,最多不要超过 3 层, 否则,代码的可读性很差】
2)实质上,嵌套循环就是把内层循环当成外层循环的循环体。当只有内层循环的循环条件为false 时,才会完全跳出内层循环,才可结束外层的当次循环,开始下一次的循环[听不懂,走案例]。
设外层循环次数为 m 次,内层为 n 次,则内层循环体实际上需要执行 m*n 次

public class MulFor {//编写一个main方法public static void main(String[] args) {for(int i = 0; i < 2; i++) { //先思考for( int j = 0; j < 3; j++) {System.out.println("i=" + i + "j=" + j);}}}}
import java.util.Scanner;public class MulForExercise01 {//编写一个main方法public static void main(String[] args) {//统计3个班成绩情况,每个班有5名同学,//求出各个班的平均分和所有班级的平均分[学生的成绩从键盘输入]。//统计三个班及格人数,每个班有5名同学。////思路分析://化繁为简//(1) 先计算一个班 , 5个学生的成绩和平均分 , 使用for//1.1 创建 Scanner 对象然后,接收用户输入//1.2 得到该班级的平均分 , 定义一个 doubel sum 把该班级5个学生的成绩累积//(2) 统计3个班(每个班5个学生) 平均分//(3) 所有班级的平均分//3.1 定义一个变量,double totalScore 累积所有学生的成绩//3.2 当多重循环结束后,totalScore / (3 * 5)//(4) 统计三个班及格人数//4.1 定义变量 int passNum = 0; 当有一个学生成绩>=60, passNum++//4.2 如果 >= 60 passNum++//(5) 可以优化[效率,可读性, 结构]//创建 Scanner 对象Scanner myScanner = new Scanner(System.in);double totalScore = 0; //累积所有学生的成绩int passNum = 0;//累积 及格人数int classNum = 3; //班级个数int stuNum = 5;//学生个数for( int i = 1; i <= classNum; i++) {//i 表示班级double sum = 0; //一个班级的总分for( int j = 1; j <= stuNum; j++) {//j 表示学生System.out.println("请数第"+i+"个班的第"+j+"个学生的成绩");double score = myScanner.nextDouble();//当有一个学生成绩>=60, passNum++if(score >= 60) {passNum++;}sum += score; //累积System.out.println("成绩为" + score);}//因为sum 是 5个学生的总成绩System.out.println("sum=" + sum + " 平均分=" + (sum / stuNum));//把 sum 累积到 totalScoretotalScore += sum;}System.out.println("三个班总分="+ totalScore+ " 平均分=" + totalScore / (classNum*stuNum));System.out.println("及格人数=" + passNum);}}

public class fordetail {public static void main(String[] args) {for (int m=1;m<=9;m++){for(int i=1;i<=m;i++){int a=i*m;System.out.print(i+"*"+m+"="+a+"\t");}System.out.println();}}}
打印空心金字塔
化繁为简 分解步骤
先打印五行* ```java public class fordetail { public static void main(String[] args) {
for(int i = 1; i <=5; i++){System.out.println("*****"); //println会自动换行}
} }
2. 打印半个金字塔<br />分解过程<br /> * //第1层 有 1个*<br /> ** //第2层 有 2个*<br /> *** //第3层 有 3个*<br /> **** //第4层 有 4个*<br /> ***** //第5层 有 5个*```javapublic class fordetail {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. 打印整个金字塔<br />分解过程<br /> * //第1层 有 1个* 2 * 1 -1 有4=(总层数-1)个空格 <br /> *** //第2层 有 3个* 2 * 2 -1 有3=(总层数-2)个空格<br /> ***** //第3层 有 5个* 2 * 3 -1 有2=(总层数-3)个空格<br /> ******* //第4层 有 7个* 2 * 4 -1 有1=(总层数-4)个空格<br />********* //第5层 有 9个* 2 * 5 -1 有0=(总层数-5)个空格<br />不计算空格的编码
public class fordetail {public static void main(String[] args) {for(int i = 1; i <=5; i++){//内循环控制打印*的个数for (int j=1; j<=2*i-1;j++){ // 2 * 层数 -1System.out.print("*");}//每打印完一个内循环就换行System.out.println("");}}}
加上空格 输出实心三角形
public class fordetail {public static void main(String[] args) {for(int i = 1; i <=5; i++){for (int k =1; k<= 5-i;k++){ // (总层数-1)个空格System.out.print(" ");}//内循环控制打印*的个数for (int j=1; j<=2*i-1;j++){System.out.print("*");}//每打印完一个内循环就换行System.out.println("");}}}
4. 打印空心的金字塔 [最难的]<br /> * //第1层 有 1个* 当前行的第一个位置是*,最后一个位置也是*<br /> * * //第2层 有 2个* 当前行的第一个位置是*,最后一个位置也是*<br /> * * //第3层 有 2个* 当前行的第一个位置是*,最后一个位置也是*<br /> * * //第4层 有 2个* 当前行的第一个位置是*,最后一个位置也是*<br />********* //第5层 有 9个* 全部输出*
public class fordetail {public static void main(String[] args) {for(int i = 1; i <=5; i++){for (int k =1; k<= 5-i;k++){System.out.print(" ");}//内循环控制打印*的个数for (int j=1; j<=2*i-1;j++){if (j == 1 || j == 2*i-1 || i == 5){ // 通过一个for循环判断是第1个 最后一个 和总数为5输出*System.out.print("*");}else{System.out.print(" "); //其余情况输出空格填充}}//每打印完一个内循环就换行System.out.println("");}}}
5先死后活 把固定值改为变量
public class Stars {//编写一个main方法public static void main(String[] args) {/*先死后活5 层数做成变量 int totalLevel = 5;*/int totalLevel = 20; //层数for(int i = 1; i <= totalLevel; i++) { //i 表示层数//在输出*之前,还有输出 对应空格 = 总层数-当前层for(int k = 1; k <= totalLevel - i; k++ ) {System.out.print(" ");}//控制打印每层的*个数for(int j = 1;j <= 2 * i - 1;j++) {//当前行的第一个位置是*,最后一个位置也是*, 最后一层全部 *if(j == 1 || j == 2 * i - 1 || i == totalLevel) {System.out.print("*");} else { //其他情况输出空格System.out.print(" ");}}//每打印完一层的*后,就换行 println本身会换行System.out.println("");}}}
