JAVA基础(14)---循环结构

版权声明:如需转载请标明出处 https://blog.csdn.net/yj201711/article/details/83621556

循环结构:

                  for循环   while循环   do...while循环

for循环

                    结构:for(初始化语句;循环条件;控制语句){
                                                     循环体;
                                  }

public class  ForDemo1{
    
    public stattic void main(String[] args){
        
        for(int i = 1 ; i <= 10 ; i++){
              
             System.out.println("当前数字为:"+ i);  
           }
     }
}

水仙花问题

/*
在控制台输出所有的”水仙花数”(
所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
   举例:153就是一个水仙花数。
   153 = 1*1*1 + 5*5*5 + 3*3*3  1 * 100 + 5 * 10 + 3
         )

*/
public class ForDemo3{
	public static void main(String[] args){
		int count = 0;
		for(int i = 100 ; i < 1000 ; i++){
			int ge =  i % 100 % 10;
			int shi = i % 100 /10;
			int bai= i /100;
			int lifang = ge * ge * ge + shi * shi * shi + bai * bai * bai;
			if(lifang == i){
				System.out.println("当前水仙花数为:" + i);
				count++;
			}
		
		}
		System.out.println("水仙花数的个数为:" + count);
	
	}
}

多重for循环

                  for(int i = 0 ; i < 10;i++){
                        for(int j = 0 ; j < 10; j++){
                               for(int k = 0 ; k < 10; k++){
                                     System.out.println("hellword");
 
                                    }
        
                          }
    
                }

public class ForForDemo1{
	public static void main(String[] args){
		/*int count = 0 ;
		for(int i = 0 ; i < 10;i++){
			for(int j = 0 ; j < 10; j++){
				for(int k = 0 ; k < 10; k++){
					
					System.out.println("hellword----" + (count++));
					
				}
			
			}
		
		}*/

		/*
			需求一:在控制台输出一个5 * 5 的一个*型图案
			* * * * *
			* * * * *
			* * * * *
			* * * * *
			* * * * *
			分析:每一行有五个 需要使用print()进行输出
			在五次输出之后需要换行  println();
			要输出五行 只需要让上边输出一行的代码执行五次就可以实现

		*/
		for(int j = 0 ; j < 5 ; j++){
			for(int i = 0 ; i < 5 ; i++ ){
				/*System.out.println("*")输出括号中的内容,在行末换行
				System.out.print("*") 输出括号中的内容,但是行末不换行
				*/
				System.out.print("* ");
			}
			System.out.print("\r\n");//使用转义字符实现换行
		}
		
		/*
			需求二:输出一个*型图案,三角形
			*
			* *
			* * *
			* * * *
			* * * * *
		*/
		for(int m = 0 ; m < 5 ; m++){
			for(int n = 0 ; n < m + 1 ; n++){
			
			System.out.print("* ");
			}
		System.out.print("\r\n");
		}
		/*
			需求三:输出一个正三角形的*型图案

                           *
                          * *
                         * * *
                        * * * *
                       * * * * *
                   */
            for(int x = 0 ; x < 5 ; x++ ){
                    for(int y = 5 ; y > x + 1 ; y--){
                            System.out.print(" ");
                        }
                    for(int z = 0 ; z < x + 1 ; z++){
                            System.out.print("*");
                        }
                } 

         
           /*
			    * ---4
			   * *--- 3
			  *   *--2
			 *     *--1
			*       * -- 0
			0 --4 5 -0 -1
			1--3  5 - 1 - 1   
			2--2	5 - 2 -1
			3--1
			4--0
			最外层 ---是控制行数的  依然是5行  不变
			在每一行需要两个输出语句  一条语句负责输出空格(for)  一条语句负责输出*(for)
	
		*/
		java.util.Scanner sc = new java.util.Scanner(System.in);
		System.out.println("请输入你要输出的图形的行数:");
		int rowNum = sc.nextInt();
		for(int x = 0 ; x < rowNum ; x++){//最外层循环, 负责行
			/*for(int y =5  ; y > x + 1 ; y--){
			
				System.out.print(" ");
			
			}*/
			for( int y = 0 ; y  < rowNum -(x +1) ; y++ ){
				System.out.print(" ");
			}
			for(int z = 0 ; z < x +1; z++){

				System.out.print("* ");
				try{
					Thread.sleep(500);//间隔1000毫秒输出下一个
				}catch(Exception e){
					e.printStackTrace();
				}
				
			
			}
			System.out.print("\r\n");
		
		}

	}
}

99乘法表

public class NineNineFor{
	public static void main(String[] args){
	/*
		输出99乘法表
	*/

		for(int i = 1 ; i < 100; i++){
			
			for (int j = 1 ; j <= i; j++ ){
				System.out.print(i +" * "+ j +" = " + i * j + " ");
			}
			System.out.println();
		}
	
	}
}

while循环

                        while(循环条件){
       
                                            }

         循环条件的结构必须是一个boolean :true  false

public class  WhileDemo{
	public static  void  main(String[] args){
		
		/*
		使用while循环来计算0--100之间的所有数的和
		for(int i = 0 ; i < 100 ; i++){
		
		}
		*/
		int i = 0;//初始化语句
		int sum = 0;
		while( i < 100){//循环条件
			sum += i ;
			i++;//控制语句
		}
		System.out.println(sum);
		System.out.println(i);


	}
}

死循环:无论是哪种循环结构,如果循环条件永远为true,那么这个循环就成为了一个死循环

死循环的利与弊:对于我们这个实际应用来说,要根据具体情况而定,作为一个真实的应用程序来讲,死循环应该是我们需要规避掉的一个问题;但是在一些特殊的领域又要求必须是一个死循环:要求程序必须一致保持在运行状态(单片机中的程序必须是死循环)

for和while 的区别:
            变量的作用域: for中定义的变量作用范围仅限于整个for循环
                                      while中的变量,因为定义在了while的外部,所以他的作用的范围就是整个main方法,在while之外也依然可以使用该变量。

如果循环结构中的变量的作用于不仅限于该循环结构,那么就使用while循环,如果变量的作用范围在循环结构之外没有任何的使用价值则使用for循环。while循环和for循环之间在大多数情况下,可以相互转换。

do...while        

                                   do{
                                            循环体 ;        //不论条件是否满足,至少会执行一次
            
                                     }while(循环条件) ;

do...while的while之后,必须要要有一个分号;只有在大括号的后边没有分号,其他语句结束之后都有分号;

import java.util.Scanner;
public class DoWhileDemo2{
	/*
		需求:针对实际应用系统,登陆,此时我们需要对用户名和密码进行判断
		如果用户名和密码不正确,应该给出用户提示,让用户重新输入
		如果正确,则进入系统
	*/
	public  static  void main(String[] args){
		//肯定需要使用do...while
		Scanner sc = new  Scanner(System.in);
		String username = null;
		String password = null;
		do{
			System.out.println("请输入您账号:");
			username = sc.next();
			System.out.println("请输入您密码:");
			password = sc.next();
		}while(!(username.equals("admin") && password.equals("123456")));

		System.out.println("登陆成功");
	}
}

while和do...while的区别:
       执行流程的区别:
                                  while先判断,后执行
                                  do...while先执行,后判断

结束循环

循环结构在一定的条件时,需要结束循环
                      break:结束当前循环(整个循环都结束,不再继续执行)
                      continue:结束本次循环,继续下次循环
                      return:结束整个方法的执行
                      结束多重for循环(跳出多重for循环):使用标签
 

public  class BreakDemo{
	public  static void  main(String[] args){
	/*跳出多重for循环的第一种形式
	STOP:		
	for(int j = 0 ; j < 10 ; j++){
		for(int i = 0 ; i < 10 ; i++){
				
				if(i == 5){
					break STOP;
				
				}
				System.out.println(i);
			
			}
			System.out.println("----------------");
	}		
*/
	//第二种方式 使用标志符 + break
	
	boolean flag = true;
	for(int j = 0 ; j < 10 && flag; j++){
		for(int i = 0 ; i < 10 ; i++){
				
				if(i == 5){
					flag = false;
					break;
				
				}
				System.out.println(i);
			
			}
			System.out.println("----------------");
	}		
	}
}

猜你喜欢

转载自blog.csdn.net/yj201711/article/details/83621556
今日推荐