java循环方法 while,do while,for循环的示例分享

java循环方法 while,do while,for循环的示例分享

转自:

http://www.java265.com/JavaJingYan/202206/16557744573791.html

循环:

 是指沿着某条搜索路线,依次对树(或图)中每个节点均做一次访问。访问结点所做的操作依赖于具体的应用问题, 具体的访问操作可能是检查节点的值、更新节点的值等。不同的遍历方式,其访问节点的顺序是不一样的。遍历是二叉树上最重要的运算之一,是二叉树上进行其它运算之基础。当然遍历的概念也适合于多元素集合的情况,如数组

循环可遍历数据,根据条件对集合中的数据进行循环

下文笔者通过示例的方式,讲述while,do while,for循环的示例分享,如下所示

while循环

public class Test {
   public static void main(String args[]) {
      int x = 8;
      while( x < 10 ) {
         System.out.print("x值 : " + x );
         x++;
         System.out.print("
");
      }
   }
}

do…while 循环

public class Test {
   public static void main(String args[]){
      int x = 8;
 
      do{
         System.out.print("x 值: " + x );
         x++;
         System.out.print("
");
      }while( x < 10 );
   }
}

for循环

public class Test {
   public static void main(String args[]) {
 
      for(int x = 8; x < 10; x = x+1) {
         System.out.print("x 值 : " + x );
         System.out.print("
");
      }
   }
}
 

Java 增强for循环

public class Test {
   public static void main(String args[]){
      int [] numbers = {88,99,10,111,222};
      for(int x : numbers ){
         System.out.print( x );
         System.out.print(",");
      }
      System.out.print("
");
      String [] names ={"java265", "java265.com", "java265.com-2", "java265.com-3"};
      for( String name : names ) {
         System.out.print( name );
         System.out.print(",");
      }
   }
}
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » java循环方法 while,do while,for循环的示例分享