level 8
Java中有三种主要的循环结构:
while 循环
do…while 循环
for 循环
2017年02月01日 03点02分
1
level 8
while 循环
while ( 布尔表达式 )
{
//循环内容
}
2017年02月01日 03点02分
3
level 8
public class Test
{
public static void main(String args[])
{
int x = 10;
while( x > 0 )
{
System.out.print("x -> " + x );
x--;
}
}
}
2017年02月01日 03点02分
4
level 8
do…while 循环
do {
//代码语句
} while ( 布尔表达式 );
public class Test
{
public static void main(String args[])
{
int x = 10;
do {
System.out.print("X -> " + x );
x--;
}while( x > 0 );
}
}
2017年02月01日 03点02分
5
level 8
for循环
for循环执行的次数是在执行前就确定的
for(初始化; 布尔表达式; 更新)
{
//代码语句
}
public class Test
{
public static void main(String args[])
{
for(int x = 0; x < 20; x++)
{
System.out.print("x -> " + x );
}
}
}
2017年02月01日 03点02分
6
level 8
Java 增强 for 循环
for(声明语句 : 表达式)
{
//代码句子
}
public class Test
{
public static void main(String args[])
{
int [] key = {10, 20, 30, 40, 50};
for(int x : key)
{
System.out.print( x );
System.out.print(",");
}
}
2017年02月01日 03点02分
7