java循环方法 while,do while,for循环的示例分享
下文笔者通过示例的方式,讲述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("\n"); } } }
do…while 循环
public class Test { public static void main(String args[]){ int x = 8; do{ System.out.print("x 值: " + x ); x++; System.out.print("\n"); }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("\n"); } } }
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("\n"); String [] names ={"java265", "java265.com", "java265.com-2", "java265.com-3"}; for( String name : names ) { System.out.print( name ); System.out.print(","); } } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。