第五章 多重循环
学习笔记分享
第一节 二重循环
1.什么是二重循环?
二重循环就是一个循环结构中又包含另外一个循环结构
while(外层循环条件){
//外层循环操作
while(内层循环条件){
//内层循环操作
}
//外层循环操作
}
while(外层循环条件){
//外层循环操作
for(循环变量初始化;内层循环变量;循环变量更新){
//内层循环操作
}
//外层循环操作
}
//省略其他循环结构的组合
2.执行流程图
3.应用场景
打印乘法表
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
分析
a.乘法表需要打印9行
b.每一行中的列数跟行号一致
代码实现
/**
* 打印乘法表
* a.乘法表需要打印9行
* b.每一行中的列数跟行号一致
*
*/
public class Example1 {
public static void main(String[] args){
for(int i=1; i<=9; i++){//打印9行
for(int j=1; j<=i; j++){//j<=i表示列数的最大值就是行号
//print表示在同一行中打印,也就是不会换行
System.out.print(j + "x" + i + "=" + i*j + "\t");
}
System.out.println();//换行
}
}
}
打印实心矩形
**********
**********
**********
**********
分析
a.矩形一共打印四行
b.每一行都有10列
代码实现
/**
* **打印实心矩形**
* a.矩形一共打印四行
* b.每一行都有10列
*
*/
public class Example2 {
public static void main(String[] args){
for(int i=0; i<4; i++){
for(int j=0; j<10; j++){
System.out.print("*");
}
System.out.println();
}
}
}
思考如何打印空心矩形?
**********
* *
* *
**********
分析
a.矩形一共打印4行
b.每一行都有10列
c.矩形的第一行和最后一行都是" * “,第一列和最后一列也都是” * "
代码实现
/**
*打印空心矩形
* a.矩形一共打印4行
* b.每一行都有10列
* c.矩形的第一行和最后一行都是" * ",第一列和最后一列也都是" * "
*
*/
public class Example3 {
public static void main(String[] args){
for(int i=0; i<4; i++){
for(int j=0; j<10; j++){
if(i == 0 || i == 3 || j == 0 || j == 9){
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
打印三角形
*
***
*****
*******
*********
分析
a.三角形一共有5行
b.三角形的第一行的列数与三角形的行数一致
c.三角形的每一行的空白数量 = 三角形的总行数 - 行号
d.三角形的每一行的*数量 = (行号 - 1)x 2
代码实现
/**
* 打印三角形
* a.三角形一共有4行
* b.三角形的第一行的列数与三角形的行数一致
* c.三角形的每一行的空白数量 = 三角形的总行数 - 行号
* d.三角形的每一行的*数量 = (行号 - 1)x 2
*
*/
public class Example4 {
public static void main(String[] args){
int totalLines = 5;//总行数
for(int i=1; i<=totalLines; i++){
int whiteSpaceCount = totalLines - i; //空白数量 = 三角形总行数 - 行号
for(int j=0; j<whiteSpaceCount; j++){
System.out.print(" ");
}
int starCount = (i - 1) * 2 + 1;// '*'数量 = (行号 - 1) * 2 + 1
for(int m =0; m< starCount; m++){
System.out.print("*");
}
System.out.println();
}
}
}
思考如何打印倒三角形?
*********
*******
*****
***
*
分析
a.三角形一共有5行
b.三角形第一行的列数 = (三角形的总行数 - 1 ) x 2 + 1
c.三角形每一行的空白数量 = 行号 - 1
d.三角形每一行的*数量 = (三角形的总行数 - 行号) x 2 + 1
代码实现
/**
* 打印倒三角形
* a.三角形一共有5行
* b.三角形第一行的列数 = (三角形的总行数 - 1 ) x 2 + 1
* c.三角形每一行的空白数量 = 行号 - 1
* d.三角形每一行的*数量 = (三角形的总行数 - 行号) x 2 + 1
*
*/
public class Example5 {
public static void main(String[] args){
int totalLines = 5;//总行数
for(int i=1; i<=totalLines; i++){
int whiteSpaceCount = i - 1; //空白数量 = 行号 - 1
for(int j=0; j<whiteSpaceCount; j++){
System.out.print(" ");
}
int starCount = (totalLines - i) * 2 + 1;
for(int m=0; m<starCount; m++){
System.out.print("*");
}
System.out.println();
}
}
}
打印菱形
*
***
*****
*******
*****
***
*
分析
a.菱形的总行数一定是奇数
b.上三角形(包含对称轴在内),打印方法与打印正三角形一样
c.下三角形每一行的空白数量 = 行号,剩下的打印方法与倒三角形相同
代码实现
/**
* 打印菱形
*
*/
public class Example6 {
public static void main(String[] args){
int totalLines = 7;//总行数必须为奇数
//先打印上三角形
int topLines = totalLines / 2 + 1;//上三角形总行数
for(int i = 1; i<=topLines; i++){
int whiteSpaceCount = topLines - i;//空白数量 = 上三角形总行数 - 行号
for(int m=0; m<whiteSpaceCount; m++){
System.out.print(" ");
}
int starCount = (i - 1) * 2 + 1;// '*'数量 = (行号 - 1) * 2 + 1
for(int m =0; m< starCount; m++){
System.out.print("*");
}
System.out.println();
}
int downLines = totalLines - topLines;//下三角形总行数
//再打印下三角形
for(int i=1; i<=downLines; i++){
//每一行的空白数量与行号相同
for(int m=0; m<i; m++){
System.out.print(" ");
}
//每一行的*数量 = (downLines - 行号) * 2 + 1
int starCount = (downLines - i) * 2 + 1;
for(int n=0; n<starCount; n++){
System.out.print("*");
}
System.out.println();
}
}
}
4.奇数
求2~100内的所有质数
分析
a.求质数的范围2~100
b.判断这个范围内的每一个数是否是质数
c.质数的特征:一个数除了1和它本身之外不能被任何数整除。换言之,就是从2开始,到这个数-1为止,没有任何一个数能够被该数整除。
代码实现
/**
* 打印2~100的所有质数
* a.求质数的范围2~100
* b.判断这个范围内的每一个数是否是质数
* c.质数的特征:一个数除了1和它本身之外不能被任何数整除。换言之,就是从2开始,到这个数-1为止,没有任何一个数能够被该数整 除。
*
*/
public class Exercise1 {
public static void main(String[] args){
for(int i=2; i<=100; i++){
if( i ==2 ){
System.out.println(i + "是质数");
} else {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
//如果一个数模上另外一个数结果为0,则表示这个数能够被另一个数整除
if(i % j == 0){
isPrime = false;
break;
}
}
if(isPrime)System.out.println(i + "是质数");
}
}
}
}
第二节 Java中的标号(标签label)
1.语法规则
标号名称:循环结构
2.作用
标号的作用就是给代码添加一个标记,方便后面使用。通常应用在循环结构中,与break语句配合使用,与C语言中的goto语句有异曲同工之妙
3.应用场景
有如下菜单:
实现其中返回主菜单 的功能
4.代码实现
import java.util.Scanner;
/**
* 实现返回主菜单
*
*/
public class Example7 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while (true){
System.out.println("************************");
System.out.println("1.学生成绩管理");
System.out.println("2.学生选课管理");
System.out.println("3.退出系统");
System.out.println("************************");
System.out.println("请选择菜单编号:");
int menuNo = sc.nextInt();
if(menuNo == 1){
childMenu: while(true) {
System.out.println("==========================");
System.out.println("1.添加成绩");
System.out.println("2.查看成绩");
System.out.println("3.修改成绩");
System.out.println("4.删除成绩");
System.out.println("5.返回主菜单");
System.out.println("==========================");
System.out.println("请选择菜单编号:");
int number = sc.nextInt();
switch(number){
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 childMenu; //Java中的标号,可以理解为一个代码的标记
//这里的break代表的是打穿上面的childMenu所在的循环
//而不是这个switch结构
}
}
} else if (menuNo == 2){
} else {
System.out.println("感谢使用本人开发的系统");
break;//终止break所在的循环
}
}
}
}
break childMenu; //Java中的标号,可以理解为一个代码的标记
//这里的break代表的是打穿上面的childMenu所在的循环
//而不是这个switch结构
}
}
} else if (menuNo == 2){
} else {
System.out.println("感谢使用本人开发的系统");
break;//终止break所在的循环
}
}
}
}