---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
循环语句
(1)while语句
class WhileDemo
{
public static void main(String[] args)
{
int i=1,s=0;//初始化语句
while (i<=100)//循环条件
{
s=s+i;//循环体
i++;//迭代语句
}
System.out.println(s);
}
}
class WhileDemo2
{
public static void main(String[] args)
{
int i=0,s=0;
while (i++<100)//++优先级大于<
{
s=s+i;
}
System.out.println(s);
}
}
(2)do while
class DoWhileDemo
{
public static void main(String[] args)
{
int i=0,s=0;
do
{
s=s+i;
i++;
}
while (i<=100);
System.out.println(s);
}
}
(4)循环嵌套
练习1:打印九九乘法表
class MultiplicationTable
{
public static void main(String[] args)
{
for (int b=1;b<10 ;b++ )
{
for (int a=1;a<=b ;a++ )
{
System.out.print(a+"*"+b+"="+a*b+"\t");
}
System.out.println();
}
}
}
/*
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
*/
控制循环结构
(1)break
class MultiplicationTable
{
public static void main(String[] args)
{
for (int b=1;b<10 ;b++ )
{
for (int a=1;a<=b ;a++ )
{
if(a==6)
break;
System.out.print(a+"*"+b+"="+a*b+"\t");
}
System.out.println();
}
}
}
/*
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45
*/
break不仅可以结束其所在的循环,还可以直接结束其外循环。此时需要break后紧跟一个标签,这个标签用于标识一个外层循环。
class MultiplicationTable
{
public static void main(String[] args)
{
Outer:
for (int b=1;b<10 ;b++ )
{
for (int a=1;a<=b ;a++ )
{
if(a==6)
break Outer;
System.out.print(a+"*"+b+"="+a*b+"\t");
}
System.out.println();
}
}
}
/*
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30
*/
(2)使用continue结束本次循环
class MultiplicationTable
{
public static void main(String[] args)
{
for (int b=1;b<10 ;b++ )
{
for (int a=1;a<=b ;a++ )
{
if(a==6)
continue;
System.out.print(a+"*"+b+"="+a*b+"\t");
}
System.out.println();
}
}
}
/*
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 7*9=63 8*9=72 9*9=81
*/
与break类似,continue也可以标识一个标签,用于标签所标识循环的当次循环
class MultiplicationTable
{
public static void main(String[] args)
{
Outer:
for (int b=1;b<10 ;b++ )
{
for (int a=1;a<=b ;a++ )
{
if(a==6)
continue Outer;
System.out.print(a+"*"+b+"="+a*b+"\t");
}
System.out.println();
}
}
}
/*
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45
*/
3)return语句
return语句是结束一个方法。return后可以跟常量,变量和表达式。
---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------