0
点赞
收藏
分享

微信扫一扫

javaSE基础(五)

1.循环结构

for 次数循环

## while 条件循环

1.1for 次数循环

语法:

	for(条件初始化;条件判断;条件变化){
	    循环体;
	}
	public class Class025_ForPractice {
public static void main(String[] args) {
	//求1-100的和,求1-100的偶数和,求1-100的奇数和
	int sum = 0;
	int sum1 = 0;  //奇数和
	int sum2 = 0;  //偶数和
	for(int i=1;i<=100;i++){
		sum+=i;
		//奇数
		if(i%2!=0){
			sum1+=i;
		}else{
			sum2+=i;
		}
	}
	System.out.println(sum);
	System.out.println("奇数和 = "+sum1);
	System.out.println("偶数和 = "+sum2);

1.2 while 条件循环

语法:

条件初始化;
		while(条件判断){
		  循环体;
			条件变化;
		}
		public class Class026_while {
public static void main(String[] args) {
	//for
	for(int i=1;i<=10;i++){
		System.out.println(i);
	}
	//while
	int i=1;
	while(i<=10){
		System.out.println(i);
		i++;
	}
	
	//1~100之间整数和
	i=1;
	int sum = 0;
	while(i<=100){
		sum+=i;
		i++;
	}
	System.out.println(sum);
	System.out.println(i);
}

}

循环之间可以相互转化

在使用while循环时候,注意条件的声明位置,条件变化的位置
while循环适合使用在:条件不用声明就已经存在|条件变化不需要手动控制,这种情况适合使用while循环
for循环适合用在: 确定循环次数,确定条件变化区间,适合使用for循环

1.3 do…while

条件初始化;
	  {
		循环体语句;
		条件变化;
	  }while(条件判断);

//请找出[100,300]之间能被5整除的所有数,每行输出8个数,使用,分隔每两个数据

		 count=0; //计数器
		 j=100;
		 // for(int i=100;i<=300;i++){
		 do {
		   if(j%5==0){
               // 每行的第一个数据,只打印数据,不打印,
                if(count==0){
                    System.out.print(j);
                }else{
                    System.out.print(","+j);
                }

                count++;

                if(count==8){
                    System.out.println();  //换行
                    count=0; //计数器归零
                }

            }
            j++;
        }while (j<=300);

while: 先判断,满足条件要求在执行每一次循环;

do whlie :先执行一次循环,然后在判断条件决定下一次是否执行

无论是否满足条件都要执行一次;

区别

    for:次数循环 --> 多 *****

	whlie: 条件循环 -> **

	do while :理解

1.4死循环 :

循环无法停止
编译器识别的死循环:

		while(true){}
		for(;;){}
		do{}while(true);
		死循环后面的代码为不可达语句

运行时期的死循环 :
后面出现了语句,编译也不会报错,因为编译器认为这个循环有可能停止

ctrl+c ==> 控制台中强制终止程序的执行

//编译器识别的死循环:
	/*
	do{

	}while(true);
	*/

    //运行时期的死循环  :
	/*
	boolean flag = true;
	while(flag){
		System.out.println("while-true");
	}
	*/
    int i=1;
    while(i<=10){
        System.out.println(i);
    }

    System.out.println("while..true之后");

1.5 break和continue

break:
作用:结束,终止
应用场景:switch,循环
continue:
作用:结束本次循环,进入下次循环
应用场景:循环中

  public class Class025_BreakContinuev {
  public static void main(String[] args) {
        //输出1~10之间的整数,如果遇到3或者3的倍数跳过
        for (int i=1;i<=10;i++){
            if (i%3==0){	
                continue;
            }
            System.out.println(i);
         //找到100,300之间前五个能被5整数的数据
        }
        int connt=0;
        for (int j=100;j<=300;j++){
            if (j%5==0){
                System.out.println(j);
                connt++;
                //结束1循环的条件
                if (connt==5){
                    break;
                }
            }
        }
    }
}

1.6循环嵌套 :

	for(条件初始化;条件判断;条件变化){
		//循环体语句
		for(条件初始化;条件判断;条件变化){
			循环体语句
		}
		//循环体语句
	}

外层循环执行一次,内存循环执行一轮(从开始到不满足循环条件结束)

 /*
   * 					1
   * *					2
   *   * 				3
   *     * 			4
   * * * * * 			5
  */
  for(int i=1;i<=5;i++){
      for(int j=1;j<=i;j++){
         if (j==1||i==5||i==j) {
               System.out.print("* ");
             }else {
                 System.out.print("  ");
      }
  }
  System.out.println();  //换行
 

//打印九九乘法表
  int sum=0;
  for (int i=1;i<=9;i++){
    for (int j=1;j<=i;j++){
    System.out.print(i+"*"+j+"="+(i*j)+"   ");

  }
  System.out.println();
举报

相关推荐

0 条评论