for循环10000次花多长时间_day02 程序流程控制 循环结构

本文深入探讨了程序流程控制中的循环结构,包括for、while和do-while循环,以及嵌套循环的使用。通过多个代码示例和演示,阐述了如何在满足特定条件时反复执行代码,并介绍了优化循环效率的技巧,如break和continue关键字的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2.5.4  程序流程控制:循环结构

循环结构

       在某些条件满足的情况下,反复执行特定代码的功能。

循环语句分类

      for循环

      while循环

      do-while循环

5abeed560f013bcf11ae86f639674fb0.png

代码1:

/*
    循环语句:
        初始化:做一些初始操作
        条件判断:让我们知道要做多少次
        循环体:就是要做的事情
        控制条件变化:通过控制条件,让我们在合适的时候结束
*/class ForDemo {public static void main(String[] args) {//在控制台输出一次"HelloWorld"
        System.out.println("HelloWorld");//在控制台输出十次"HelloWorld"
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");//在控制台输出一万次"HelloWorld"
    }

代码2:

/*
    for循环的格式:
        for(初始化语句;判断条件语句;控制条件语句) {
            循环体语句;
        }
        执行流程:
        A:首先执行初始化语句
        B:其次执行判断条件语句,看其返回值
            如果是true,就继续
            如果是false,循环结束
        C:执行循环体语句
        D:执行控制条件语句
        E:回到B
*/class ForDemo2 {public static void main(String[] args) {//在控制台输出10次HelloWorldfor(int x=0; x<10; x++) {
            System.out.println("HelloWorld");
        }   
        System.out.println("--------------");//初始化不从0开始for(int x=1; x<=10; x++) {
            System.out.println("HelloWorld");
        }   for(int x=1; x<11; x++) {
            System.out.println("HelloWorld");
        }for(int x=10; x>0; x--) {
            System.out.println("HelloWorld");
        }
    }
}

代码3:

/*
    需求:求5的阶乘
    阶乘:
        n! = n*(n-1)*(n-2)*...*3*2*1
        n! = n*(n-1)!
*/class ForDemo3 {    public static void main(String[] args) {//定义累乘变量int jc = 1;for(int x=1; x<=5; x++) {
            jc *= x;
        }
        System.out.println("5的阶乘是:"+jc);
    }

代码4:

/*
    需求:统计”水仙花数”共有多少个
    分析:
        A:我们要统计有多少个满足条件的数据,就要定义一个统计变量
            int count = 0;
        B:一个三位数其实告诉我们的是范围,通过for循环就可以搞定。
        C:其各位数字的立方和等于该数本身就是规则
            我们如何取得每一个位上的数据呢?
            给了任意的一个数据x 153
            个位:x%10
            十位:x/10%10
            百位:x/10/10%10
            千位:x/10/10/10%10
            ...
            x == (个位*个位*个位 + 十位*十位*十位 + 百位*百位*百位)
*/class ForDemo4 {public static void main(String[] args) {//定义统计变量int count = 0;for(int x=100; x<1000; x++) {int ge = x%10;int shi = x/10%10;int bai = x/10/10%10;if(x == (ge*ge*ge + shi*shi*shi + bai*bai*bai)) {
                count++;
            }
        }
        System.out.println("水仙花数共有:"+count+"个");
    }
}

代码5:

class ForTest {public static void main(String[] args) {//ÇëÔÚ¿ØÖÆÌ¨Êä³öÊý¾Ý1-10
        System.out.println(1);
        System.out.println(2);
        System.out.println(3);
        System.out.println(4);
        System.out.println(5);
        System.out.println(6);
        System.out.println(7);
        System.out.println(8);
        System.out.println(9);
        System.out.println(10);
        System.out.println("------------");for(int x=0; x<10; x++) {
            System.out.println(x+1);    
        }
        System.out.println("------------");for(int x=1; x<=10; x++) {
            System.out.println(x);  
        }
        System.out.println("------------");for(int x=10; x>0; x--) {
            System.out.println(x);  
        }
        System.out.println("------------");
    }
}

代码6:

/*
    需求:求出1-10之间数据之和
        0+1=1
            1+2=3
                3+3=6
                    6+4=10
                        10+5=15
                            ...
        因为每一次的累加结果都是变化的,所以要定义一个变量,专门用于记录每次累加的结果。
        由于我们需要的1,2,3,4...也是变化的,所以我们也要定义一个变量,而这个变量用循环就能得到每个值。
*/class ForTest2 {public static void main(String[] args) {//最好想//System.out.println(1+2+3+4+5+6+7+8+9+10);//跟循环结合起来int sum = 0;for(int x=1; x<=10; x++) {//x=1,2,3,4,...10//sum = sum + x; //sum=0 + 1 = 1;//sum = sum + x; //sum=1 + 2 = 3;//sum = sum + x;
            sum += x;
        }
        System.out.println("1-10的和是:"+sum);
    }

代码7:

class ForTest3 {public static void main(String[] args) {//求出1-100之间偶数和/*
        //定义求和变量
        int sum = 0;
        //通过for循环获取每一个数据
        for(int x=1; x<=100; x++) {
            //把数据累加
            sum += x;
        }
        //输出结果
        System.out.println("1-100之和:"+sum);
        System.out.println("---------------");
        *///偶数:能被2整除的数据//如何判断数据是否能够被整出呢? x%2 == 0/*
        int sum = 0;
        for(int x=1; x<=100; x++) {
            if(x%2 == 0) {
                sum += x;
            }
        }
        System.out.println("1-100的偶数和:"+sum);
        */int sum = 0;for(int x=0; x<=100; x+=2) {
            sum += x;
        }
        System.out.println("1-100的偶数和:"+sum);
    }
}

代码8:

/*
    所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
    举例:153就是一个水仙花数。
    153 = 1*1*1 + 5*5*5 + 3*3*3
    分析:
        A:一个三位数其实告诉我们的是范围,通过for循环就可以搞定。
        B:其各位数字的立方和等于该数本身就是规则
            我们如何取得每一个位上的数据呢?
            给了任意的一个数据x 153
            个位:x%10
            十位:x/10%10
            百位:x/10/10%10
            千位:x/10/10/10%10
            ...
            x == (个位*个位*个位 + 十位*十位*十位 + 百位*百位*百位)
*/
class ForTest4 {
    public static void main(String[] args) {
        for(int x=100; x<1000; x++) {
            int ge = x%10;
            int shi = x/10%10;
            int bai = x/10/10%10;

            if(x == (ge*ge*ge + shi*shi*shi + bai*bai*bai)){
                System.out.println(x);
            }
        }
    }
}

代码9:

/*
    需求:请在控制台输出满足如下条件的五位数
            个位等于万位
            十位等于千位
            个位+十位+千位+万位=百位
    分析:
        A:五位数告诉我们范围。
        B:获取每一个位上的数据。
        C:满足条件
            个位等于万位
            十位等于千位
            个位+十位+千位+万位=百位
*/class ForTest5 {public static void main(String[] args) {for(int x=10000; x<100000; x++) {int ge = x%10;int shi = x/10%10;int bai = x/10/10%10;int qian = x/10/10/10%10;int wan = x/10/10/10/10%10;if((ge == wan) && (shi == qian) && (ge+shi+qian+wan == bai)) {
                System.out.println(x);
            }
        }
    }

代码10:

/*
    需求:请统计1-1000之间同时满足如下条件的数据有多少个:
            对3整除余2
            对5整除余3
            对7整除余2
    分析:
        A:定义一个统计变量。
        B:1-1000之间告诉我们了范围,用for循环可以解决
        C:条件
            对3整除余2
            对5整除余3
            对7整除余2
            x%3 == 2
            x%5 == 3
            x%7 == 2
*/class ForTest6 {public static void main(String[] args) {//定义一个统计变量。int count = 0;//1-1000之间告诉我们了范围,用for循环可以解决for(int x=1; x<=1000; x++) {//判断条件if(x%3==2 && x%5==3 && x%7==2) {//System.out.println(x);
                count++;
            }
        }
        System.out.println("共有"+count+"个满足条件的数据");
    }
}

演示代码1:

package day04;/**
 * @author yun~
 * For循环结构的使用
 * 一,循环结构的四个要素
 * ①初始条件
 * ②循环条件     -->是Boolean类型
 * ③循环体
 * ④迭代条件
 *
 * 二,for循环的结构
 * for(①;②;④){
 *     ③
 * }
 *
 * 执行过程:① - ② - ③ - ④ - ② - ③ - ④ - ... - ②
 *
 */public class ForTest {public static void main(String[] args){/*
        //多次重复一句话。
        for(int i=0; i<0; i++){
            System.out.println("Hello World!");
        }
        //i:在for循环内有效。出了for循环就失效了。
        //System.out.println(i);
        //练习:
        int num = 1;
        for(System.out.print('a'); num<=3; System.out.println('c'),num++){
            System.out.print('b');
        }
        //输出结果:= abcbcbc.
         *///例题:遍历100以内的偶数,//输出所有偶数的和。int sum = 0;//输出偶数的个数int count = 0;for(int i=1; i<=100; i++){if(i % 2 ==0){
                System.out.println(i);
                sum+=i;
                count++;
            }//System.out.println("总和为:"+sum);
        }
        System.out.println("总和为:"+sum);
        System.out.println("个数为:"+count);
    }
}

演示代码2:

package day04;/**
 * @author yun
 * 编写程序从1循环到150,并在每行打印一个值。
 * 另外在每个3的倍数上打印出“foo",
 * 在每个5的倍数上打印”biz",
 * 在每个7的倍数上打印出“baz".
 */public class ForTest1 {public static void main(String[] args){for(int i=1; i<=150; i++){//遍历i的每个数字
            System.out.print(i+" ");if(i % 3 ==0){
                System.out.print("foo ");
            }if(i % 5 ==0){
                System.out.print("biz ");
            }if(i % 7 ==0){
                System.out.print("baz ");
            }//换行,每循环一次换一次行,以免在一行内打印过长,也不美观。
            System.out.println("");
        }
    }
}class ForTest6 {public static void main(String[] args) {//定义一个统计变量。int count = 0;//1-1000之间告诉我们了范围,用for循环可以解决for(int x=1; x<=1000; x++) {//判断条件if(x%3==2 && x%5==3 && x%7==2) {//System.out.println(x);
                count++;
            }
        }
        System.out.println("共有"+count+"个满足条件的数据");
    }
}

演示代码3:

package day05;import java.util.Scanner;/**
 * @author yun
 * 题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数。
 *  比如:12和20的最大公约数是4,最小公倍数是60.
 *     最大公约数:12 20 /4,
 *     最小公倍数:12 = 2 * 2 * 3
 *                20 = 2 * 2 * 5
 *                2 * 2 * 3 * 5
 *
 *                16 = 2 * 2 * 4
 *                32 = 4 * 4 * 2
 *                2 * 4 * 4 * 2
 *
 * 说明:break关键字的使用。
 */public class ForTest {public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入第一个正整数:");int m = scan.nextInt();
        System.out.println("请输入第二个正整数:");int n = scan.nextInt();//获取两个数中的最小值int min = (m <=n)? m:n;//遍历for(int i=min; i>=1; i--){if(m % i ==0 && n % i ==0){
                System.out.println(i);//一旦在循环中执行到break,就跳出当前循环。break;
            }
        }//获取m和n的最小公倍数//1.获取两个数中最大值int max = (m >=n)? m:n;//遍历for(int i= max; i            if(i % m==0 && i % n==0){
                System.out.println(i);break;
            }
        }
    }
}

2-5-5  循环结构2:  while

代码1:

/*
    while循环的格式:
        while(条件表达式) {
            语句体;
        }
    变形格式:
        初始化语句;
        while(判断条件语句) {
            循环体语句;
            控制条件语句;
        }
        for(初始化语句;判断条件语句;控制条件语句) {
             循环体语句;
        }
    由此可见while循环和for循环是可以等价转换的。
*/class WhileDemo {public static void main(String[] args) {//for/*
        for(int x=0; x<10; x++) {
            System.out.println("HelloWorld");
        }
        System.out.println("--------------");
        *///while/*
        int x=0;
        while(x<10) {
            System.out.println("HelloWorld");
            x++;
        }
        *///求和//for/*
        int sum = 0;
        for(int x=1; x<=100; x++) {
            sum+=x;
        }
        System.out.println("1-100之和是:"+sum);
        *///while/*
        int sum = 0;
        int x = 1;
        while(x<=100) {
            sum += x;
            x++;
        }
        System.out.println("1-100之和是:"+sum);
        *///水仙花//for/*
        for(int x=100; x<1000; x++) {
            int ge = x%10;
            int shi = x/10%10;
            int bai = x/10/10%10;
            if(x == (ge*ge*ge + shi*shi*shi + bai*bai*bai)) {
                System.out.println(x);
            }
        }
        *///whileint x = 100;while(x<1000) {int ge = x%10;int shi = x/10%10;int bai = x/10/10%10;if(x == (ge*ge*ge + shi*shi*shi + bai*bai*bai)) {
                System.out.println(x);
            }
            x++;
        }
    }

代码2:

/*
    for和while的区别:
    使用区别:控制条件语句所控制的那个变量,在for循环结束后,就不能再被访问到了,
              而while循环结束还可以继续使用,如果你想继续使用,就用while,否则推荐使用for。
              原因是for循环结束,该变量就从内存中消失,能够提高内存的使用效率。
    场景区别:
            for循环适合针对一个范围判断进行操作
                水仙花
            while循环适合判断次数不明确操作
                吃葡萄
*/class WhileDemo2 {public static void main(String[] args) {int x = 0;while(x<10) {
            System.out.println(x);
            x++;
        }
        System.out.println(x+"---");
        System.out.println("-----------");for(int y=0; y<10; y++) {
            System.out.println(y);
        }//System.out.println(y+"---");
    }
}

代码3:

/*
    我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,厚度为:0.01m。
    请问,我折叠多少次,就可以保证厚度不低于珠穆朗玛峰的高度?
    分析:
        A:初始化条件
            我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,厚度为:0.01m。
        B:统计变量
            我折叠多少次
        C:判断条件
            纸张厚度<=8848
        D:变化的数据
            纸张厚度每折叠一次是以前的2倍
*/class WhileTest {public static void main(String[] args) {//初始条件int end = 884800;int start = 1;//定义统计变量int count = 0;//while循环while(start<=end) {
            count++;//纸张厚度每折叠一次是以前的2倍
            start*=2;
        }
        System.out.println("需要折叠:"+count+"次");//这道题目用for循环改/*
        int count = 0;
        for(int start = 1,end = 884800; start<=end;start*=2){
            count++;
        }
        System.out.println("需要折叠:"+count+"次");
        */
    }
}

代码4:

/*
    我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,厚度为:0.01m。
    请问,我折叠多少次,就可以保证厚度不低于珠穆朗玛峰的高度?
    分析:
        A:初始化条件
            我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,厚度为:0.01m。
        B:统计变量
            我折叠多少次
        C:判断条件
            纸张厚度<=8848
        D:变化的数据
            纸张厚度每折叠一次是以前的2倍
*/class WhileTest {public static void main(String[] args) {//初始条件int end = 884800;int start = 1;//定义统计变量int count = 0;//while循环while(start<=end) {
            count++;//纸张厚度每折叠一次是以前的2倍
            start*=2;
        }
        System.out.println("需要折叠:"+count+"次");//这道题目用for循环改/*
        int count = 0;
        for(int start = 1,end = 884800; start<=end;start*=2){
            count++;
        }
        System.out.println("需要折叠:"+count+"次");
        */
    }
}

演示代码:

package day05;/**
 * @author yun
 * 一、循环条件的4个要素
 * ①初始化条件
 * ②循环条件       --->是boolean类型
 * ③循环体
 * ④迭代条件
 *
 * 二、while循环的结构
 * ①
 * while(②){
 *     ③;
 *     ④;
 * }
 *
 * 执行过程:① - ② - ③ - ④ - ② - ③ - ④ - ② - ... - ②(直到false).
 *
 * 说明:
 * 1.写while循环千万小心不要丢了迭代条件。一旦丢了,就可能导致死循环。
 * 2.我们写程序,要避免出现死循环。
 * 3.for循环和while循环是可以相互转换的!
 *   区别:for循环和while循环的初始化条件部分的作用范围不同。
 *
 * 算法:有限性。算法中每条指令的执行次数是有限的,执行每条指令的时间也是有限的。
 * 也就是说算法必须在有限的时间内可以完成,要不然就没有现实意义
 */public class WhileTest {public static void main(String[] args){//遍历100以内的所有偶数int i = 1;while(i <= 100){if(i % 2 == 0){
                System.out.println(i);
            }
            i++;
        }//101,出了while循环以后,仍可以调用。
        System.out.println(i);
    }
}

2-5-6 循环结构3:  do-while循环

代码1:

/*
    do...while格式:
        do {
            语句体;
        }while(条件表达式);
    变形格式:
        初始化语句;
        do {
            循环体语句;
            控制条件语句;
        }while(判断条件语句);
        for(初始化语句;判断条件语句;控制条件语句) {
             循环体语句;
        }
*/class DoWhileDemo {public static void main(String[] args) {/*
        int sum = 0;
        for(int x=1; x<=100; x++) {
            sum+=x;
        }
        System.out.println(sum);
        *///do...whileint sum = 0;int x = 1;do{
            sum+=x;
            x++;
        }while (x<=100);
        System.out.println(sum);
    }

代码2:

/*
    三种循环的区别:
        A:do…while循环至少会执行一次循环体。
        B:for循环和while循环只有在条件成立的时候才会去执行循环体。
    注意事项:
        A:写程序优先考虑for循环,再考虑while循环,最后考虑do…while循环。
        B:最简单的死循环代码
            for(;;){}
*/class DoWhileTest {public static void main(String[] args) {/*
        for(int x = 10; x>20; x++) {
            System.out.println("hello");
        }
        int y = 10;
        while(y>20){
            System.out.println("world");
            y++;
        }
        int z = 10;
        do{
            System.out.println("java");
            z++;
        }while(z>20);
        *//*
        for(;;){
            System.out.println("haha");
        }
        */while(true) {
            System.out.println("haha");
        }
    }
}

演示代码1:

package day05;/**
 * @author yun
 * do-while循环的使用
 * 一、循环条件的4个要素
 *  ①初始化条件
 *  ②循环条件       --->是boolean类型
 *  ③循环体
 *  ④迭代条件
 *
 *  二、do-while循环结构
 *  ①
 *  do{
 *      ③;
 *      ④;
 *  }while(②);
 *
 *  执行过程:① - ③ - ④ - ② - ③ - ④ - ② -... - ②
 *  说明:
 *  1.do-while循环至少会执行一次循环体!
 *  2.开发中,使用for和while更多一些,较少使用do-while
 */public class DoWhileTest {public static void main(String[] args){//遍历100以内的偶数,并计算所有偶数的和及偶数的个数。int num = 1;//记录总和int sum = 0;//记录个数int count = 0;do{if(num % 2 == 0){
                System.out.println(num+" ");
                sum += num;
                count++;
            }
            num++;
        }while(num <= 100);
        System.out.println("总和为:"+sum);
        System.out.println("个数为:"+count);//********************************************int number1 = 10;while(number1 > 10){
            System.out.println("hello:while");
            number1--;
        }//至少执行一次循环体,执行多次都是一样的。int number2 = 10;do{
            System.out.println("hello:while");
            number1--;
        }while (number1 > 10);int number3 = 10;for(int i=0; i<10; i++){
            System.out.println("hello:while");
            number1--;
        }
    }
}

演示代码2:

package day05;import java.util.Scanner;/**
 * @author yun
 * 题目:
 * 从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。
 *
 * 说明:
 * 1.不在循环条件部分限制次数的结构:for(;;) 或 while(true)
 * 2.结束循环有几种方式
 *    方式一:循环条件部分返回false
 *    方式二:在循环体中,执行break
 */public class ForWhileTest {public static void main(String[] args){//①从键盘读入个数不确定的正数
        Scanner scan = new Scanner(System.in);//正数个数计数器int count1 = 0;//负数个数计数器int count2 = 0;//输入数int num=0;//for(;;){}while(true){
            System.out.println("请输入正数:(负数或整数)");
            num = scan.nextInt();if(num >=0){
                count1++;
            }
            count2++;if(num == 0) {break;
            }
        }do{
            System.out.println("请输入正数:(负数或整数)");
            num = scan.nextInt();//②并判断读入的正数和负数的个数if(num >= 0){
                count1++;
            }
            count2++;
        }while(num==0);
        System.out.println("正数个数:"+count1);
        System.out.println("负数个数:"+count2);
    }
}

2-5-7 嵌套循环

                   (嵌套循环)多重循环

●将一个循环放在另一个循环体内,就形成了嵌套循环。其中,for,while,do...while均可以作为外层循环或内层循环。
●实质上,嵌套循环就是把内层循环当成外层循环的循环体。当只有内层循环的循环条件为false时,才会完全跳出内层循环,才可以结束外层的当次循环,开始下一次的循环。
●设外层循环次数为m次,内层为n次,则内层循环体实际上需要执行m*n次。

代码1:

/*
    需求:请输出一个4行5列的星星(*)图案。
            *****
            *****
            *****
            *****
    输出语句的问题:
        System.out.println("*");
            在当前行输出*,并换行。
        System.out.print("*");
            仅仅在当前行输出*
    循环嵌套中:
        外循环控制行数
        内循环控制列数
*/class ForForDemo {public static void main(String[] args) {//方式一/*
        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("*");
        System.out.println("*");
        System.out.print("*");
        System.out.print("*");
        System.out.println();
        *//*
        System.out.print("*");
        System.out.print("*");
        System.out.print("*");
        System.out.print("*");
        System.out.print("*");
        *///用循环改进一行输出5个*/*
        //第一行
        for(int x=0; x<5; x++) {
            System.out.print("*");
        }
        System.out.println();
        //第二行
        for(int x=0; x<5; x++) {
            System.out.print("*");
        }
        System.out.println();
        //第三行
        for(int x=0; x<5; x++) {
            System.out.print("*");
        }
        System.out.println();
        //第四行
        for(int x=0; x<5; x++) {
            System.out.print("*");
        }
        System.out.println();
        *///同样的事情做了很多次for(int y=0; y<4; y++) {for(int x=0; x<5; x++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

代码2:

/*
    需求:请输出如下图形
            *
            **
            ***
            ****
            *****
*/class ForForDemo2 {public static void main(String[] args) {//我们先输出一个5行5列的星形/*
        for(int x=0; x<5; x++) {
            for(int y=0; y<5; y++) {
                System.out.print("*");
            }
            System.out.println();
        }
        *///而我们现在需要的是下半部分//通过观察图形:我们知道行没有变化,是列在变化//第一行:1列//第二行:2列//第三行:3列//第四行:4列//第五行:5列//既然是一个变化的数,那么我们就定义一个变量/*
        int z = 0;
        for(int x=0; x<5; x++) {
            for(int y=0; y<=z; y++) {
                System.out.print("*");
            }
            System.out.println();
            z++;
        }
        *///我们发现z的变化和x的变化其实是一致的//所以我们根本就没有必要定义z变量,直接把z用x替换for(int x=0; x<5; x++) {for(int y=0; y<=x; y++) {
                System.out.print("*");
            }
            System.out.println();
        }
        System.out.println("---------");for(int x=1; x<=5; x++) {for(int y=1; y<=x; y++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

代码3:

/*
    需求:在控制台输出九九乘法表。
        1*1=1
        1*2=2   2*2=4
        1*3=3   2*3=6   3*3=9
        ...
        1*9=9   2*9=18  3*9=27  4*9=36  ...
    转移字符:
        \t  tab键的位置
*/class ForForDemo3 {public static void main(String[] args) {//如果我们把每一行看作一颗*//那么这其实就是我们刚才的三角形/*
        for(int x=1; x<=9; x++) {
            for(int y=1; y<=x; y++) {
                System.out.print("*"+"\t");
            }
            System.out.println();
        }
        *///接下来,我们要把*替换为表达式for(int x=1; x<=9; x++) {for(int y=1; y<=x; y++) {
                System.out.print(y+"*"+x+"="+(x*y)+"\t");
            }
            System.out.println();
        }
    }
}

演示代码1:

package day05;/**
 * @author yun
 * 嵌套循环 for-for
 * 嵌套循环的使用
 * 1.嵌套循环: 将一个循环结构A声明在另一个循环结构B的循环体中,
 *             就构成了嵌套循环。
 * 2.
 * 外层循环: 循环结构B
 * 内层循环:  循环结构A
 *
 * 3.说明:
 * ①内层循环结构遍历一遍,只相当于外层循环 循环体执行了一次。
 * ②假设外层循环需要执行m次,内层循环需要执行n次,此时内层
 *  循环的循环体一共执行了m*n次。
 *
 * 4.技巧
 *    外层循环控制行数,内层循环控制列数。
 */public class ForForTest {public static void main(String[] args){//******//System.out.println("******");for(int i=1; i<=6; i++){
            System.out.print("*");
        }
        System.out.println("\n");//******//******//******//******for(int i=1; i<=4; i++){for(int j=1; j<=6; j++){
                System.out.print('*');
            }
            System.out.println("");
        }
        System.out.println("\n");/*
          *
          * *
          * * *
          * * * *
          * * * * *
         *///控制行数for(int i=1; i<=5; i++){//控制列数for(int j=1; j<=i; j++){
                System.out.print("* ");
            }
            System.out.println("\n");
        }
        System.out.println("*************************\n");/*                  i(行号)       J(个数)  规律:i + j =5
            * * * * *       1             5       换句话说:j = 5 - i;
            * * * *         2             4
            * * *           3             3
            * *             2             2
            *               1             1
         */for(int i=1; i<=5; i++){for(int j=1; j<=5-i; j++){
                System.out.print("*");
            }
            System.out.println("");
        }
        System.out.println("**************************\n");/*                 规律: 行     个    总结:j = i
         *                        1     1
         * *                      2     2
         * * *
         * * * *
         * * * * *
         * * * * *                1    5      总结:j=i-1
         * * * *                  2    4           j=i-2
         * * *                    3    3
         * *
         *
         */for(int i=1; i<=5; i++){for(int j=1; j<=i; j++){
                System.out.print("*");
            }
            System.out.println("");
        }for(int i=1; i<=5; i++){for(int k=1; k<=5-i; k++){
                System.out.print("*");
            }
            System.out.println("");
        }
    }
}

演示代码2:

package day05;/**
 * @author yun
 * 嵌套循环 for-for
 * 九九乘法表
 */public class NineNineTable {public static void main(String[] args){/*
           1 * 1 = 1                                 1   1
           1 * 2 = 2   2 * 2 = 4                     2   2
           1 * 3 = 3   2 * 3 = 6     3 * 3 = 9       3   3
         */for(int i=1; i<=9; i++){for(int j=1; j<=i; j++){
                System.out.print(j+" * "+i+" = "+(i*j)+"  ");
            }
            System.out.println();
        }
    }
}

演示代码3:

package day05;/**
 * @author yun
 * 100以内的所有的质数的输出。
 * 质数:素数,只能被1和他本身整除的自然数。
 *       -->从2开始,到这个数-1结束为止,都不能被这个数本身整除。
 * 最小的质数是:2
 */public class PrimeNumberTest {public static void main(String[] args){//标识i是否被j除尽,一旦除尽,修改其值。boolean isFlag = true;//从2过后开始,遍历100以内的自然数for(int i=2; i<=100; i++){//J:被i去除for(int j=2; j                 //i被j除尽if(i % j == 0){
                    isFlag = false;
                }
            }//判断if(isFlag == true){
                System.out.println(i);
            }//重置isFlag
            isFlag = true;
        }
    }
}

优化一:

package day05;/**
  * @author yun
  * 100以内的所有的质数的输出。
  * 质数:素数,只能被1和他本身整除的自然数。
  *       -->从2开始,到这个数-1结束为止,都不能被这个数本身整除。
  * 最小的质数是:2
 *  对PrimeNumberTest.java文件中质数输出问题的优化。
 */public class PrimeNumberTest1 {public static void main(String[] args){//标识i是否被j除尽,一旦除尽,修改其值。boolean isFlag = true;//获取当前时间距离 1970-01-01 00:00:00 的毫秒数long start = System.currentTimeMillis();//从2过后开始,遍历100000以内的自然数for(int i=2; i<=100000; i++){//J:被i去除for(int j=2; j                 //i被j除尽if(i % j == 0){
                    isFlag = false;//优化一:只对本身非质数的自然数是有效的。break;
                }
            }//判断if(isFlag == true){
                System.out.println(i);
            }//重置isFlag
            isFlag = true;
        }//获取当前时间距离1970-01-01 00:00:00 的毫秒数long end = System.currentTimeMillis();//没有优化的时间:20105(20秒),优化后:2585
        System.out.println("所花费的时间为: "+(end-start));
    }
}

优化2:

package day05;/**
  * @author yun
  * 100000以内的所有的质数的输出。
  * 质数:素数,只能被1和他本身整除的自然数。
  *       -->从2开始,到这个数-1结束为止,都不能被这个数本身整除。
  * 最小的质数是:2
 *  对PrimeNumberTest.java文件中质数输出问题的优化。
 */public class PrimeNumberTest1 {public static void main(String[] args){//标识i是否被j除尽,一旦除尽,修改其值。boolean isFlag = true;//记录质数的个数int count = 0;//获取当前时间距离 1970-01-01 00:00:00 的毫秒数long start = System.currentTimeMillis();//从2过后开始,遍历100000以内的自然数for(int i=2; i<=100000; i++){//J:被i去除//优化二:对本身是质数的自然数是有效的,//for(int j=2; jfor(int j=2; j                 //i被j除尽if(i % j == 0){
                    isFlag = false;//优化一:只对本身非质数的自然数是有效的。break;
                }
            }//判断if(isFlag == true){//输出拖慢了程序的速度//System.out.println(i);
                count++;
            }//重置isFlag
            isFlag = true;
        }//获取当前时间距离1970-01-01 00:00:00 的毫秒数long end = System.currentTimeMillis();
        System.out.println("质数的个数为: "+count);//没有优化的时间:20105(20秒),//优化后:2585,第二次优化:1014,
        System.out.println("所花费的时间为: "+(end-start));
    }
}

2-5-8  特殊关键字的使用: break、continue

代码1:

/*
    根据要求填空
*/class BreakAndContinue {public static void main(String[] args) {for(int x=1; x<=10; x++) {if(x%3==0) {//在此处填写代码break;//continue;//System.out.println("Java基础班");
            }
            System.out.println("Java基础班");
        }//我想在控制台输出2次:“Java基础班“//我想在控制台输出7次:“Java基础班“//我想在控制台输出13次:“Java基础班“ 
    }
}

代码2:

/*
    break:中断
    break的使用场景:
        A:在选择结构switch语句中
        B:在循环语句中
        离开使用场景的存在是没有意义的
    break:
        A:跳出单层循环
        B:跳出多层循环(了解)
            带标签的用法
*/class BreakDemo {public static void main(String[] args) {for(int x=0; x<10; x++) {//System.out.println(x);if(x%2==0) {break;//break后面是不能有东西的//System.out.println(x);
            }
            System.out.println(x);
        }
        System.out.println("-------------");
        wc:for(int x=0; x<3; x++) {
            nc:for(int y=0; y<4; y++) {
                System.out.print("*");break;
            }
            System.out.println();
        }
    }

代码3:

/*
    continue的使用场景:
        在循环语句中
        离开使用场景的存在是没有意义的
    continue的作用:
        A:单层循环对比break,然后总结两个的区别
            break  退出当前循环
            continue  退出本次循环
        B:也可以带标签的使用
*/class ContinueDemo {public static void main(String[] args) {//continue;for(int x=0; x<10; x++) {if(x%2==0) {//break;  //结束当前循环continue; //结束本次循环操作,进入下一次操作
            }
            System.out.println(x);
        }
        wc:for(int x=0; x<3; x++) {
            nc:for(int y=0; y<4; y++) {
                System.out.print("*");continue wc;
            }
            System.out.println();
        }
    }
}


演示代码1:

package day05;/**
 * @author yun
 * break和continue关键字的使用:
 *             使用范围:         循环中使用的作用(不同点)      相同点
 * break:      switch-case                              break,continue后面不能放语句
 *             循环结构中         结束当前循环
 *
 * continue:   循环结构中         结束本次循环
 *
 */public class BreakContinueTest {public static void main(String[] args){for(int i=0; i<= 10; i++){//i被4%为0,则进入if(i % 4==0){//break;continue;
            }//break:结果:1,2,3//continue:结果:1,2,3,5,6,7,9
            System.out.println(i);
        }
        System.out.println();//
        label:for(int i=1; i<=4; i++){for(int j=1; j<=10; j++){if(j % 4 ==0){//默认跳出包裹此关键字最近的一层循环。//break;//默认跳出包裹此层循环的一次循环。//continue;//结束指定标识的一层循环结构//break label;//结束指定标识的一层循环结构当次循环continue label;
                }
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

演示代码2:

package day05;/**
 * @author yun
 * 100000以内的所有的质数的输出。   实现方式二:for标签
 * 质数:素数,只能被1和他本身整除的自然数。
 *       -->从2开始,到这个数-1结束为止,都不能被这个数本身整除。
 * 最小的质数是:2
 *  对PrimeNumberTest.java文件中质数输出问题的优化。
 */public class PrimeNumberTest2 {public static void main(String[] args){//标识i是否被j除尽,一旦除尽,修改其值。boolean isFlag = true;//记录质数的个数int count = 0;//获取当前时间距离 1970-01-01 00:00:00 的毫秒数long start = System.currentTimeMillis();//从2过后开始,遍历100000以内的自然数
        label:for(int i=2; i<=100000; i++){for(int j=2; j                 //i被j除尽if(i % j == 0){continue label;
                }
            }//执行到此步骤的,都是质数。
            count++;
        }//获取当前时间距离1970-01-01 00:00:00 的毫秒数long end = System.currentTimeMillis();//9592
        System.out.println("质数的个数为: "+count);//没有优化的时间:20105(20秒),//优化后:2585,第二次优化:1014,第三次优化:13
        System.out.println("所花费的时间为: "+(end-start));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值