zoukankan      html  css  js  c++  java
  • 第九章 循环结构进阶段

    一、本章目标

    • 掌握二重循环的使用

    • 掌握二重循环中跳转语句的使用

    二、知识点

    1 二重循环

    1.1 回顾循环

    问题:某次程序大赛,S1班有4名学员参加,学员的成绩由用户输入,计算该班参赛学员的平均分 

                

    实现:

    1. /**

    2. * 计算一个班的平均分

    3. */

    4. public class AvgScore {

    5. public static void main(String args[]){

    6. int[] score = new int[4]; //成绩数组

    7. float sum = 0.0f; //成绩总和

    8. float average = 0.0f; //成绩平均值

    9. //循环输入学员成绩

    10. Scanner input = new Scanner(System.in);

    11. System.out.println("请输入4位学员的成绩");

    12. for(int i = 0; i < score.length; i++){

    13. System.out.print("第"+ (i+1) +"位学员的成绩:");

    14. score[i] = input.nextInt();

    15. sum = sum + score[i];     //成绩累加

    16. }

    17. average = sum / score.length; //计算平均值

    18. System.out.println("参赛学员的平均分是:" + average);

    19. }

    20. }

    1.2 为什么使用二重循环

    问题:若有3个班级各4名学员参赛,如何计算每个班级参赛学员的平均分?

                

    分析:

        使用二循环

        外层循环控制班级数目,内层循环控制每个班级学员数目

    1.3 什么是二循环

        一个循环体内又包含另一个完整的循环结构 

        

    特点:外层循环变量变化一次,内层循环变量要变化一遍

    1.4 使用二重循环

    解决上面的问题

    1. /**
    2. * 计算3个班级的平均分
    3. */
    4. public class AvgScore {
    5. public static void main(String args[]){
    6. int[] score = new int[4]; //成绩数组
    7. int classNum = 3; //班级数目
    8. double sum = 0.0; //成绩总和
    9. double average = 0.0; //平均成绩
    10. //循环输入学员成绩
    11. Scanner input = new Scanner(System.in);
    12. for(int i = 0; i < classNum; i++){
    13. sum = 0.0f; //成绩总和归0
    14. System.out.println("请输入第" + (i+1) + "个班级的成绩");
    15. for(int j = 0; j < score.length; j++){
    16. System.out.print("第" + (j+1) + "个学员的成绩:");
    17. score[j] = input.nextInt();
    18. sum = sum + score[j]; //成绩累加
    19. }
    20. average = sum / score.length; //计算平均分
    21. System.out.println("第" + (i+1) + "个班级参赛学员的平均分是:" + average + " ");
    22. }
    23. }
    24. }
    问题二:打印矩形图案

    分析:

    • 用二重循环实现

      • 外层循环控制行数

      • 内层循环控制每行的*号数

    实现

    1. /**
    2. * 打印矩形
    3. */
    4. public class Rectangle {
    5. public static void main(String[] args) {
    6. System.out.println("打印矩形");
    7. for(int i = 0; i < 5; i++){
    8. for(int j = 0; j <5; j++){
    9. System.out.print("*");
    10. }
    11. System.out.print(" "); //换行
    12. }
    13. }
    14. }

    1.5 技能训练

    训练1:打印直角三角形

    问题:打印直角三角形 

    • 从控制台输入直角三角形的高度(行数)

    • 每行 * 的数目依次为1、3、5、7…

    分析:

        1.外层循环控制行数 

        2.分析每行打印的内容

        3.每一行打印字符*结束后要换行

        4.内层循环的条件: j<=2i-1

    实现

    1. /**
    2. * 输入行数打印直角三角形
    3. */
    4. public class RTriAngle {
    5. public static void main(String[] args) {
    6. int rows = 0; //三角形行数
    7. System.out.print("请输入直角三角形的行数:");
    8. Scanner input = new Scanner(System.in);
    9. rows = input.nextInt();
    10. //打印直角三角形
    11. for(int i = 1; i <= rows; i++){
    12. for(int j = 1; j <= 2*i-1; j++){
    13. System.out.print("*");
    14. }
    15. System.out.print(" ");
    16. }
    17. }
    18. }

    训练二:打印倒直角三角形

    需求说明:

    • 从控制台输入直角三角形的高度(行数)

    • 每行*的数目从下至上依次为1、2、3、4…

    实现

    1. /**
    2. * 输入行数打印倒直角三角形
    3. */
    4. public class InvertRTriAngle {
    5. public static void main(String[] args) {
    6. int rows = 0; //三角形行数
    7. System.out.print("请输入直角三角形的行数:");
    8. Scanner input = new Scanner(System.in);
    9. rows = input.nextInt();
    10. //打印倒直角三角形
    11. for(int i = 1; i <= rows; i++){
    12. for(int j = 1; j <= rows+1-i; j++){
    13. System.out.print("*");
    14. }
    15. System.out.print(" ");
    16. }
    17. }
    18. }

    训练三:打印等腰三角形

    需求说明

    • 从控制台输入等腰三角形的高度

    • 每行*的数目依次为1、3、5、7… 

            

    分析:

    • 外层循环控制行数

    • 每行先打印空格,再打印*

    • 打印空格和字符*用两个不同的循环

    实现

    1. /**
    2. * 输入行数打印等腰三角形
    3. */
    4. public class IsoTriangle {
    5. public static void main(String[] args) {
    6. int rows = 0; //三角形行数
    7. System.out.print("请输入等腰三角形的行数:");
    8. Scanner input = new Scanner(System.in);
    9. rows = input.nextInt();
    10. //打印等腰三角形
    11. for(int i = 1; i <= rows; i++){
    12. for(int j = 1; j <= rows-i; j++){
    13. System.out.print(" ");
    14. }
    15. for(int k = 1; k <= 2*i-1; k++){
    16. System.out.print("*");
    17. }
    18. System.out.print(" ");
    19. }
    20. }
    21. }

    训练四:打印九九乘法表

    需求说明

    • 利用二重循环实现九九乘法表

    分析:

        1.九九乘法表共有9行,因此外层循环条件为

                    i<=9

        2.第i行上有i个式子,因此因此外层循环条件为

                 j <=i

        3.第i行上的第j个式子为

                     j的值 * i的值 = j*i的值

    实现

    1. /**
    2. * 打印九九乘法表
    3. */
    4. public class MulTable {
    5. public static void main(String[] args) {
    6. int rows = 9; //乘法表的行数
    7. for(int i = 1; i<=rows; i++){ //一共9行
    8. for(int j = 1; j <= i; j++){ //第i行有i个式子
    9. System.out.print(j+"*"+i+"="+j*i+" ");
    10. }
    11. System.out.print(" "); //打印完一行后换行
    12. }
    13. }
    14. }

    2 跳转语句进阶

    2.1 在二重循环中使用continue 

    问题:

        若有3个班级各4名学员参赛,计算每个班级参赛学员平均分,统计成绩大于85分学员数 

    分析

    • 在问题1基础上增加了新功能

    • 使用continue统计大于85分的学员人数

    实现

    1. /**
    2. * continue断点演示:计算成绩85分以上的学员人数
    3. *
    4. */
    5. public class ContinueDemo {
    6. public static void main(String[] args) {
    7. int[] score = new int[4]; //成绩数组
    8. int classnum = 3; //班级数目
    9. double sum = 0.0; //成绩总和
    10. double average = 0.0; //平均成绩
    11. int count = 0; //记录85分以上学员人数
    12. //循环输入学员成绩
    13. Scanner input = new Scanner(System.in);
    14. for(int i = 0; i < classnum; i++){
    15. sum = 0.0f; //成绩总和归0
    16. System.out.println("请输入第" + (i+1) + "个班级的成绩");
    17. for(int j = 0; j < score.length; j++){
    18. System.out.print("第" + (j+1) + "个学员的成绩:");
    19. score[j] = input.nextInt();
    20. sum = sum + score[j]; //成绩累加
    21. if(score[j] < 85){ //成绩小于85,则跳出本轮循环
    22. continue;
    23. }
    24. count++;
    25. }
    26. average = sum / score.length;
    27. System.out.println("第" + (i+1) + "个班级参赛学员的平均分是:" + average + " ");
    28. }
    29. System.out.println("成绩85分以上的学员人数有" + count + "人");
    30. }
    31. }

    2.2 在二重循环中使用break 

    问题:

    • 有5家衣服专卖店,每家最多购买3件。用户可以选择离开,可以买衣服

    • 最后打印总共买了几件衣服 

    分析:

    • 使用二重循环解决

      • 外层循环控制去每个专卖店

      • 内层循环控制买衣服过程

      • 使用break退出内层循环

    实现

    1. /**
    2. * break断点演示:实现购物结账
    3. */
    4. public class BreakDemo {
    5. public static void main(String[] args) {
    6. int count = 0;    //记录一共买了几件衣服
    7. String choice;    //顾客选择是否离开
    8. Scanner input = new Scanner(System.in);
    9. for(int i = 0; i < 5; i++){
    10. System.out.println("欢迎光临第" + (i+1) + "家专卖店");
    11. for(int j = 0; j < 3; j++){
    12. System.out.print("要离开吗(y/n)?");
    13. choice = input.nextLine();
    14. if("y".equals(choice)){    //如果离开,则跳出,进入下一家店
    15. break;
    16. }
    17. System.out.println("买了一件衣服");
    18. count++;
    19. }
    20. System.out.println("离店结账 ");
    21. }
    22. System.out.println("总共买了" + count + "件衣服");
    23. choice = input.nextLine();
    24. }
    25. }

    2.3 二重循环中continue和break对比 

    • continue:继续本层下一轮循环

    • break:跳出本层循环

    2.4 技能训练

    训练一:统计打折商品数量

    需求说明:

    • 有3名顾客去商场购物,每人买3件商品

    • 商品单价300元以上的商品享受8折优惠

    • 请统计每人享受打折优惠的商品的数量

    分析:

    • 外层循环条件:i<3

    • 内层循环条件:j<3

    • 使用continue统计享受优惠的商品数量

    实现

    1. /**
    2. * 统计打折商品数量
    3. */
    4. public class Demo {
    5. public static void main(String[] args) {
    6. int count = 0;    //记录打折商品数量
    7. Scanner input = new Scanner(System.in);
    8. double price=0.0; //商品价格
    9. for(int i = 0; i < 3; i++){
    10. System.out.println("请输入第"+(i+1) +"个人所购的三件商品的价格:");
    11. for(int j = 0; j < 3; j++){
    12. price=input.nextDouble();
    13. if(price<300)
    14. continue;
    15. count++ ; //累计
    16. }
    17. System.out.println("第"+(i+1) +"个人共有" +count + "件商品享受8折优惠!");
    18. count=0;//重置count值
    19. }
    20. }
    21. }

    3 作业

    1 打印数字组成的直角三角形


    实现

    1. public class NumberTriangle {
    2. /**
    3. * 打印数字组成的直角三角形
    4. */
    5. public static void main(String[] args) {
    6. int rows = 5;
    7. for(int i = 1; i <= rows; i++){
    8. for(int j = 1; j <= i; j++){
    9. System.out.print(j);
    10. }
    11. System.out.print(" ");
    12. }
    13. }
    14. }

    2 百钱买百鸡,5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡。现在用100文钱买100只鸡,那么公鸡、母鸡、雏鸡各多少只?


    实现

    1. public class Chook {
    2. /**
    3. * 百钱买百鸡
    4. */
    5. public static void main(String[] args) {
    6. int way = 1; //买法
    7. int k = 0; //雏鸡数
    8. for(int i=1;i<=20;i++){ //公鸡数
    9. for(int j=1;j<=33;j++){ //母鸡数
    10. k = 100-i-j; //一共100只鸡
    11. if(k%3 == 0 && (5*i+3*j+k/3 == 100)){//雏鸡数是3的倍数,总计100文钱
    12. System.out.print("[买法 " + way++ + "] ");
    13. System.out.println("公鸡: " +i+ " 母鸡:" +j+ " 雏鸡:" +k);
    14. }
    15. }
    16. }
    17. }
    18. }

    3 有3个班级各4名学员参赛,从控制台输入每个班级参赛学员的成绩,要求统计3个班级所有参赛学员成绩大于85分的学员的平均分,如何编程

    实现

    1. /**
    2. * continue断点演示:计算成绩85分以上的学员人数
    3. *
    4. */
    5. public class AvgScore {
    6. public static void main(String[] args) {
    7. int[] score = new int[4]; //成绩数组
    8. int classnum = 3; //班级数目
    9. double sum = 0.0; //成绩总和
    10. double average = 0.0; //平均成绩
    11. int count = 0; //记录85分以上学员人数
    12. //循环输入学员成绩
    13. Scanner input = new Scanner(System.in);
    14. for(int i = 0; i < classnum; i++){
    15. System.out.println("请输入第" + (i+1) + "个班级的成绩");
    16. for(int j = 0; j < score.length; j++){
    17. System.out.print("第" + (j+1) + "个学员的成绩:");
    18. score[j] = input.nextInt();
    19. if(score[j] < 85){ //成绩小于85,则跳出本轮循环
    20. continue;
    21. }
    22. sum = sum + score[j]; //成绩85分以上才进行累加
    23. count++;
    24. }
    25. }
    26. average = sum / count; //所有成绩85分以上的学员的平均分
    27. System.out.println("所有成绩85分以上的学员的平均分为:" + average);
    28. }
    29. }

    4 假设一个简单的ATM的取款过程如下:首先提示用户输入密码,最多只能输入3次,超过3次则提示用户“密码错误,请取卡”,结束交易。如果用户密码正确,再提示用户输入金额,ATM只能输出100元的纸币,一次取钱数要求最低0元,最高1000元。如果用户输入的金额符合上述要求,则打印输出用户取的钱数,最后提示用户“交易完成,请取卡”,否则提示用户重新输入金额。假设用户密码是111111.

    实现:

    1. public class ATMDemo {
    2. /**
    3. * 简单ATM机取款过程模拟
    4. */
    5. public static void main(String[] args) {
    6. String pass = ""; //保存用户输入密码
    7. int amount = 0; //取款金额
    8. String password = "111111"; //用户密码
    9. int count = 0; //记录密码输入次数
    10. boolean isPass = false; //密码是否通过验证
    11. Scanner input = new Scanner(System.in);
    12. while(count < 3 && !isPass){
    13. System.out.print("请输入密码:");
    14. pass = input.next();
    15. if(!password.equals(pass)){
    16. count++;
    17. continue;
    18. }
    19. isPass = true; //密码通过验证
    20. System.out.print("请输入金额:");
    21. amount = input.nextInt();
    22. while(amount>0){
    23. if(amount<=1000 && amount%100==0){
    24. System.out.println("您取了" +amount+ "元");
    25. System.out.println("交易完成,请取卡!");
    26. break; //完成交易,退出
    27. }else{
    28. System.out.print("您输入金额的金额不合法,请重新输入:");
    29. amount = input.nextInt();
    30. continue; //继续让用户输入金额
    31. }
    32. }
    33. }
    34. if(!isPass){ //用户输入了3次错误密码
    35. System.out.print("密码错误,请取卡!");
    36. }
    37. }
    38. }

    5 输入行数,打印菱形,要求如下:

    • 从控制台输入菱形的高度(行数)。如果用户输入的行数合法(奇数),则打印出菱形,否则提示用户输入奇数

    • 假设用户输的行数为rows,则每行字符*的个数依次为1,3,5,7...,rows,...7,5,3,1

    实现

    1. /**
    2. * 输入行数打印菱形
    3. */
    4. public class Diamond {
    5. public static void main(String[] args) {
    6. int rows = 0; //菱形的行数
    7. Scanner input = new Scanner(System.in);
    8. System.out.print("请输入菱形行数:");
    9. rows = input.nextInt();
    10. while(rows%2 == 0){
    11. System.out.print("请输入奇数:");
    12. rows = input.nextInt();
    13. }
    14. int n = (rows+1)/2;
    15. //打印菱形的上半部分
    16. for(int i = 1; i <= n; i++){//外层循环变量i控制行数
    17. for(int j = 1; j <= n-i; j++){//内层循环变量j控制该行空格数
    18. System.out.print(" ");
    19. }
    20. for(int k = 1; k <= 2*i-1; k++){//内层循环变量k控制该行*号数
    21. System.out.print("*");
    22. }
    23. System.out.print(" ");
    24. }
    25. //打印菱形的下半部分
    26. for(int i = n-1; i >= 1; i--){
    27. for(int j = 1; j <= n-i; j++){
    28. System.out.print(" ");
    29. }
    30. for(int k = 1; k <= 2*i-1; k++){
    31. System.out.print("*");
    32. }
    33. System.out.print(" ");
    34. }
    35. }
    36. }

    总结

    • 二重循环是一个循环体内又包含另一个完整的循环结构

    • 在二重循环中,外层循环变量变化一次,内层循环变量要从初始值到结束值变化一遍

    • 在二重循环中可以使用break、continue语句控制程序的执行

      关注我们


      捐赠我们

          良师益友工作室一直在致力于帮助编程爱好更加快速方便地学习编程,如果您对我们的成果表示认同并且觉得对你有所帮助,欢迎您对我们捐赠^_^。
          



  • 相关阅读:
    用pelican搭建完美博客
    对比MySQL,什么场景MongoDB更适用
    客官,您的 Flask 全家桶请收好
    虚拟机安装macos 分辨率不正常修改不了,不能全屏如何解决
    mac开启HiDPI
    虚拟机安装MacOS|unlocker解锁出现闪退问题!
    下载com.vmware.fusion.tools.darwin.zip.tar慢
    VMware虚拟机安装黑苹果MacOS Mojave系统详细教程
    Python格式化输出
    ubuntu进入initramfs,系统黑屏
  • 原文地址:https://www.cnblogs.com/imentors/p/4677380.html
Copyright © 2011-2022 走看看