day06
(一)循环语句
1.for循环嵌套
需求1:打印以下图形
****
****
****
for(int i = 0;i<3;i++){//控制行数
for(int j = 0;j<4;j++){//控制列数
System.out.print("*");
}
System.out.println();//换行
}
需求2:打印以下图形
* i=0
** i=1
*** i=2
**** i=3
***** i=4
for(int i = 0;i<5;i++){
for(int j = 0;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
需求3:打印以下图形
*
**
***
****
*****
for(int i = 0;i<5;i++){
for(int k = 0;k<4-i;k++){
System.out.print(" ");
}
for(int j = 0;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
需求4:打印以下图形
*****
****
***
**
*
for(int i = 0;i<5;i++){
for(int j = 0;j<5-i;j++){
System.out.print("*");
}
System.out.println();
}
需求5:打印以下图形
*****
****
***
**
*
for(int i = 0;i<5;i++){
for(int k = 0;k<i;k++){
System.out.print(" ");
}
for(int j = 0;j<5-i;j++){
System.out.print("*");
}
System.out.println();
}
需求6:打印以下图形
*
***
*****
*******
for(int i = 0;i<4;i++){
for(int k = 0;k<3-i;k++){
System.out.print(" ");
}
for(int j = 0;j<i*2+1;j++){
System.out.print("*");
}
System.out.println();
}
需求7:打印以下图形
*
* *
* *
*******
for(int i = 0;i<4;i++){
for(int k = 0;k<3-i;k++){
System.out.print(" ");
}
for(int j = 0;j<i*2+1;j++){
//第一行 || 最后一行 || 每行的第一列 || 每行的最后一列(以形成空心)
if(i==0 || i==3 || j==0 || j==i*2){//最后一列j==i*2+1取不到要减1,亦或因为从0开始没有取等,也正是没有取等简化表达式
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
需求8:打印以下图形
*******
*****
***
*
for(int i = 0;i<4;i++){
for(int k = 0;k<i;k++){
System.out.print(" ");
}
for(int j = 0;j<7-2*i;j++){
System.out.print("*");
}
System.out.println();
}
需求9:打印以下图形
*******
* *
* *
*
for(int i = 0;i<4;i++){
for(int k = 0;k<i;k++){
System.out.print(" ");
}
for(int j = 0;j<7-2*i;j++){
//第一行 || 最后一行 || 每行的第一列 || 每行的最后一列
if(i==0 || i==3 || j==0 || j==7-2*i-1){//最后一列j==7-i*2取不到要减1,亦或因为从0开始没有取等,也正是没有取等简化表达式
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
需求10:九九乘法表
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
for(int i = 1;i<=9;i++){//九九乘法表从1开始
for(int j = 1;j<=i;j++){//不一定有九列
System.out.print(j + "x" + i + "=" + (i*j) + "\t");//制表符对齐
}
System.out.println();
}
需求11:九九乘法表
1x1=1 1x2=2 1x3=3 1x4=4 1x5=5 1x6=6 1x7=7 1x8=8 1x9=9
2x2=4 2x3=6 2x4=8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18
3x3=9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27
4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36
5x5=25 5x6=30 5x7=35 5x8=40 5x9=45
6x6=36 6x7=42 6x8=48 6x9=54
7x7=49 7x8=56 7x9=63
8x8=64 8x9=72
9x9=81
for(int i = 1;i<=9;i++){
for(int k = 1;k<i;k++){
System.out.print("\t");
}
for(int j = i;j<=9;j++){//一定有九列,而且是从i列到9列
System.out.print(i + "x" + j + "=" + (i*j) + "\t");
}
System.out.println();
}
2.while和do-while
while
1.语法结构:
while(表达式){
…代码块/循环体…
}
2.理解:
表达式的结果必须是boolean类型
true - 执行代码块
false - 跳出循环
3.死循环:
while(true){
System.out.println(“死循环”);
}
4.需求:
使用while循环遍历5遍"干就完了"
int i = 1;
while(i<=5){
System.out.println("干就完了");
i++;
}
5.案例:
我有个梦想,每月存3000,每年递增1000元,多少个月后存满20万
int allMoney = 0; int money = 3000; int month = 0; while(allMoney < 200000){ allMoney += money; month++; if(month % 12 == 0){ money += 1000; } } System.out.println(month + "个月后存满20万"); System.out.println(money);
do-while
1.语法结构:
do{
…代码块/循环体…
}while(表达式);
2.理解:
首先执行一遍代码块,再判断表达式
表达式的结果必须是boolean类型
true - 执行代码块
false - 跳出循环
3.死循环:
do{
System.out.println(“死循环”);
}while(true);
做实验:
do{
System.out.println(“用良心做教育”);
}while(false);
4.案例:
奇男子参加学校组织的歌咏比赛,大赛在即,
老师建议:先彩排一次,如果很令人满意,
以后就不用彩排了,否则每天都排,直到现场表现满意为止!Scanner scan = new Scanner(System.in); String str; do{ System.out.println("奇男子:\"旋转、跳跃,我闭着眼~~~\""); System.out.println("奇男子:\"老师,您满意了吗?\""); str = scan.next(); }while(str.equals("不满意"));
3.for vs while vs do-while
表达式的区别:
for(初始化变量;判断条件;更新变量){}
while(判断条件){}
do{}while(判断条件);
共同点:判断条件的结果必须是boolean类型,true就执行代码块,false就跳出循环
执行顺序的区别:
for:先判断,再执行
while:先判断,再执行
do-while:先执行一遍,再判断
应用场景的区别:
循环次数确定 – for
循环次数不确定,并且先判断再执行 - while
循环次数不确定,先执行一遍,再判断 - do-while
(二)特殊的流程控制语句
分类:
break
continue
return
label
break
1.理解:
作用在循环中,表示跳出整个循环语句
做实验:
while(true){
System.out.println(“111”);
System.out.println(“222”);
if(true){
break;
}
System.out.println(“333”);
}
2.案例:
循环录入奇男子同学5门课的成绩并计算平均分, 如果某分数录入为负,停止录入并提示。
Scanner scan = new Scanner(System.in);
boolean flag = true;//true-正常录入 false-非正常录入
double sum = 0;
for(int i = 1;i<=5;i++){
System.out.println("请输入第" + i + "门成绩:");
double score = scan.nextDouble();
if(score < 0){
flag = false;
break;
}
sum += score;
}
if(flag){
double avg = sum/5;
System.out.println("平均分为:" + avg);
}else{
System.out.println("分数为负数,停止录入");
}