循环进阶---->一环套一环

二重循环的几种语法:

//while与while循环嵌套
        while (循环条件1) {
            //循环操作1
            while (循环条件2) {
                //循环操作2
            }
        }
        //do-while与do-while循环嵌套
        do {
            //循环操作1
            do {
                //循环操作2
            } while (循环条件2);
        } while (循环条件1);
        
        
        //for与for循环嵌套
        for (循环条件1) {
            //循环操作1
            for (循环条件2) {
                //循环操作2
                }
        }
        //while与for循环嵌套
        while (循环条件1) {
            //循环操作1
            for (循环条件2) {
                //循环操作2
                
            }
            
        }
        

上例子环节:

package cn.jbit.nestedloops;
/**
 * 打印九九乘法表
 */
public class MulTable {
    public static void main(String[] args) {
        int rows = 9;                        //乘法表的行数
        for(int i = 1; i<=rows; i++){        //一共9行
            for(int j = 1; j <= i; j++){    //第i行有i个式子
                System.out.print(j+"*"+i+"="+j*i+"    ");
            }
            System.out.print("
");            //打印完一行后换行
        }
    }
}
package cn.jbit.nestedloops;
import java.util.Scanner;
/**
 * 输入行数打印等腰三角形
 */
public class IsoTriangle {
    public static void main(String[] args) {
        int rows = 0;    //三角形行数
        System.out.print("请输入等腰三角形的行数:");
        Scanner input = new Scanner(System.in);
        rows = input.nextInt();
        //打印等腰三角形
        for(int i = 1; i <= rows; i++){
            for(int j = 1; j <= rows-i; j++){
                System.out.print(" ");
            }
            for(int k = 1; k <= 2*i-1; k++){
                System.out.print("*");
            }
            System.out.print("
");
        }
    }
}
package cn.jbit.nestedloops;
import java.util.Scanner;
/**
 * 输入行数打印倒直角三角形
 */
public class InvertRTriAngle {
    public static void main(String[] args) {
        int rows = 0;    //三角形行数
        System.out.print("请输入直角三角形的行数:");
        Scanner input = new Scanner(System.in);
        rows = input.nextInt();
        
        //打印倒直角三角形
        for(int i = 1; i <= rows; i++){
            for(int j = 1; j <= rows+1-i; j++){
                System.out.print("*");
            }
            System.out.print("
");
        }
    }
}
package cn.jbit.nestedloops;
import java.util.Scanner;
/**
 * 输入行数打印直角三角形
 */
public class RTriAngle {
    public static void main(String[] args) {
        int rows = 0;    //三角形行数
        System.out.print("请输入直角三角形的行数:");
        Scanner input = new Scanner(System.in);
        rows = input.nextInt();
        
        //打印直角三角形
        for(int i = 1; i <= rows; i++){
            for(int j = 1; j <= 2*i-1; j++){
                System.out.print("*");
            }
            System.out.print("
");
        }
    }
}
package cn.jbit.nestedloops;
import java.util.Scanner;
/**
 * 计算一个班的平均分
 */
public class AvgScore {
    public static void main(String args[]){
        int[] score = new int[4];        //成绩数组
        float sum = 0.0f;                //成绩总和
        float average = 0.0f;            //成绩平均值
        
        //循环输入学员成绩
        Scanner input = new Scanner(System.in);
        System.out.println("请输入4位学员的成绩");
        for(int i = 0; i < score.length; i++){
            System.out.print("第"+ (i+1) +"位学员的成绩:");
            score[i] = input.nextInt();
            sum = sum + score[i];         //成绩累加
        }
        average = sum / score.length;    //计算平均值
        System.out.println("参赛学员的平均分是:" + average);
    }
}
package cn.jbit.nestedloops;
import java.util.Scanner;
/**
 * 统计打折商品数量
 */
public class Demo {
    public static void main(String[] args) {
        int count = 0;    //记录打折商品数量
        Scanner input = new Scanner(System.in);
        double price=0.0; //商品价格    
        for(int i = 0; i < 3; i++){
            System.out.println("请输入第"+(i+1) +"个人所购的三件商品的价格:");
            for(int j = 0; j < 3; j++){
                price=input.nextDouble();
                if(price<300)
                    continue;
                count++ ; //累计
            }            
            System.out.println("第"+(i+1) +"个人共有" +count + "件商品享受8折优惠!");
            count=0;//重置count值
        }
    }
}
package cn.jbit.nestedloops;

public class Chook {

    /**
     * 百钱买百鸡
     */
    public static void main(String[] args) {
        int way = 1;    //买法
        int k = 0;    //雏鸡数
        for(int i=1;i<=20;i++){        //公鸡数
            for(int j=1;j<=33;j++){    //母鸡数
                k = 100-i-j;        //一共100只鸡
                if(k%3 == 0 && (5*i+3*j+k/3 == 100)){//雏鸡数是3的倍数,总计100文钱
                    System.out.print("[买法 " + way++ + "] ");
                    System.out.println("公鸡: " +i+ "    母鸡:" +j+ "     雏鸡:" +k);

                }
            }
        }

    }

}
package cn.jbit.nestedloops;
import java.util.Scanner;
/**
 * continue断点演示:计算成绩85分以上的学员人数
 * 
 */
public class AvgScore {
    public static void main(String[] args) {
        int[] score = new int[4];                //成绩数组
        int classnum = 3;                        //班级数目
        double sum = 0.0;                        //成绩总和
        double average = 0.0;                    //平均成绩
        int count = 0;                            //记录85分以上学员人数
        
        //循环输入学员成绩
        Scanner input = new Scanner(System.in);
        for(int i = 0; i < classnum; i++){
            System.out.println("请输入第" + (i+1) + "个班级的成绩");
            for(int j = 0; j < score.length; j++){
                System.out.print("第" + (j+1) + "个学员的成绩:");
                score[j] = input.nextInt();
                if(score[j] < 85){    //成绩小于85,则跳出本轮循环
                    continue;
                }
                sum = sum + score[j];            //成绩85分以上才进行累加
                count++;
            }        
        }
        average = sum / count;                    //所有成绩85分以上的学员的平均分
        System.out.println("所有成绩85分以上的学员的平均分为:" + average);
    }
}
package cn.jbit.nestedloops;
import java.util.Scanner;
public class ATMDemo {
    /**
     * 简单ATM机取款过程模拟
     */
    public static void main(String[] args) {
        String pass = "";                //保存用户输入密码
        int amount = 0;                    //取款金额
        String password = "111111";        //用户密码
        int count = 0;                    //记录密码输入次数
        boolean isPass = false;            //密码是否通过验证
        Scanner input = new Scanner(System.in);
        
        while(count < 3 && !isPass){
            System.out.print("请输入密码:");
            pass = input.next();
            if(!password.equals(pass)){
                count++;
                continue;
            }
            isPass = true;                //密码通过验证
            System.out.print("请输入金额:");
            amount = input.nextInt();
            while(amount>0){
                if(amount<=1000 && amount%100==0){
                    System.out.println("您取了" +amount+ "元");
                    System.out.println("交易完成,请取卡!");
                    break;                //完成交易,退出
                }else{
                    System.out.print("您输入金额的金额不合法,请重新输入:");
                    amount = input.nextInt();
                    continue;            //继续让用户输入金额
                }
            }
        }
        if(!isPass){                    //用户输入了3次错误密码
            System.out.print("密码错误,请取卡!");
        }
    }
}
package cn.jbit.nestedloops;
import java.util.Scanner;
/**
 * 输入行数打印菱形
 */
public class Diamond {
    public static void main(String[] args) {
        int rows = 0;    //菱形的行数
        Scanner input = new Scanner(System.in);
        System.out.print("请输入菱形行数:");
        rows = input.nextInt();

        while(rows%2 == 0){
            System.out.print("请输入奇数:");
            rows = input.nextInt();
        }

        int n = (rows+1)/2;
        //打印菱形的上半部分
        for(int i = 1; i <= n; i++){//外层循环变量i控制行数
            for(int j = 1; j <= n-i; j++){//内层循环变量j控制该行空格数
                System.out.print(" ");
            }
            for(int k = 1; k <= 2*i-1; k++){//内层循环变量k控制该行*号数
                System.out.print("*");
            }
            System.out.print("
");
        }
        //打印菱形的下半部分
        for(int i = n-1; i >= 1; i--){
            for(int j = 1; j <= n-i; j++){
                System.out.print(" ");
            }
            for(int k = 1; k <= 2*i-1; k++){
                System.out.print("*");
            }
            System.out.print("
");
        }
    }
}
原文地址:https://www.cnblogs.com/bocai2010000/p/6690169.html