# 循环结构
1. while循环
? ?while(布尔表达式){
? ???? ?//循环体
? ?}
package day04;
public class demo1 {
public static void main(String[] args) {
int i = 0;
while ( i < 100){
i++;
System.out.println(i);
}
}
}
//计算1-100和
package day04;
public class demo2 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while ( i <= 100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
? ?死循环
? ?while(true){
? ???? ?//等待客户端连接
? ???? ?//定时检查
? ?}
2. do while循环
? ?do{
? ???? ?//代码语句
? ?}while(布尔表达式)
? ?//循环体至少执行一次
package day04;
public class demo3 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do{
sum = sum + i;
i++;
}while ( i <= 100);
System.out.println(sum);
}
}
//while 、do while?
package day04;
public class demo4 {
public static void main(String[] args) {
int a = 0;
while ( a < 0 ){
System.out.println(a);
a++;
}
System.out.println("-------------");//输出------------
do{ //0
System.out.println(a);
a++;
}while ( a < 0 );
}
}
3. For循环
? ?for(初始化;布尔表达式;更新){
? ???? ?//代码语句
? ?}
? ?for循环语句是支持迭代的一种通用结构同时是最有效最灵活的循环结构
//计算0到100之间的奇数偶数和
package day04;
public class demo5 {
public static void main(String[] args) {
//计算0到100之间的奇数偶数和
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i <= 100; i++) {
if( i % 2 != 0){
oddSum += i;
}else{
evenSum += i;
}
}
System.out.println(oddSum);
System.out.println(evenSum);
}
}
//打印1-1000之间能被5整除的数,每行打印三个?
package day04;
public class demo6 {
public static void main(String[] args) {
//打印1-1000之间能被5整除的数,每行打印三个
for (int i = 1; i <= 1000; i++) {
if( i % 5 == 0){
System.out.print(i + "\t");
}
if( i % (5*3) == 0){
System.out.println();
//System.out.print("\n");
}
}
}
}
?//99乘法表
package day04;
public class demo7 {
public static void main(String[] args) {
//99乘法表
//行,共9行
for (int i = 1; i <= 9; i++) {
//列,每行j列
for (int j = 1; j <=i; j++) {
System.out.print(j +"*"+ i +"="+ (i*j) + "\t" );
}
System.out.println();//每行换行
}
}
}
4. 增强for循环
? ?for(声明语句:表达式){ ??? ?//数组、集合
? ???? ??? ?//代码语句
? ?}
package day04;
public class demo8 {
public static void main(String[] args) {
//增强for循环
//定义数组
int[] number= {10,20,30,40,50};
//遍历数组元素
for(int x : number){
System.out.println(x);
}
}
}
5. break、continue
? ?break用于强行退出循环,不执行循环中剩余的语句
package day04;
public class demo9 {
public static void main(String[] args) {
//break应用
int i = 0;
while( i <= 10 ){
i++;
System.out.println(i);//1,2,3,4,5
if( i == 5 ){
break;//跳出循环
}
}
System.out.println("123");//123
}
}
? ?continue用于终止某次循环,跳过尚未执行的语句,接着进行下一次循环判断
package day04;
public class demo10 {
public static void main(String[] args) {
//continue应用
int i = 0;
while( i < 10 ){
i++;
if( i%2 == 0 ){
continue;
}
System.out.println(i);//1,3,5,7,9
}
}
}
? ?goto关键字
? ?在break、continue中能看到goto影子,带标签:label
package day04;
public class demo11 {
public static void main(String[] args) {
//打印1-50之间的质数
//质数只能被1和本身整除的数
outer:for( int i = 1; i <= 50; i++){
for( int j = 2; j <= i/2; j++){
if( i % j == 0){
continue outer;
}
}
System.out.print(i+" ");
}
}
}
//练习题 打印三角
package day04;
public class demo12 {
public static void main(String[] args) {
//打印三角
//5行
for (int i = 1; i <= 5; i++) { //顺序执行
for (int j = 5; j >= i; j--) { //第一步
System.out.print(" ");
}
for (int j = 1; j <= i; j++) { //第二步
System.out.print("*");
}
for (int j = 1; j < i; j++) { //第三步
System.out.print("*");
}
System.out.println(); //每行换行
}
}
}
|