循环语句基本结构及应用

循环语句是指满足特定条件下,程序重复做某个动作的行为。

一:for循环语句(确切知道循环次数或者循环次数较少)

(1)标准结构

for(变量初始化;变量满足条件;变量变化规律){

语句;

}

(2)死循环结构

for(;;){

           语句;

}

(3)多变量for循环语句

for(int  i=0,j=100;i<j;i++,j++){

           语句;

}

(4)基本结构

for(int i=0;i<3;i++){
System.out.print("Hoole Java");//语句
}

该程序表示,定义一个整型变量i,并且i初始化为1;满足i<3的情况下运行语句,运行结束后,i自增1。直至i<3条件不成立,跳出循环。

所以该程序为                  

      i=0;

执行语句。

    i=1;

执行语句。

    i=2;

执行语句。

跳出循环。
例如:
<pre name="code" class="javascript">package cn.java.lhb.syntax;

/**
 * 
 * @author lhb
 *
 */
public class ForDemo {
	public static void main(String[] args) {
		for (int i = 0; i <= 100; i++) {
				System.out.print(i + "\t");
		}
	}
}


 该程序输出结果为:0,1,2,3.....99,100 

(5)循环套用条件语句

for(变量初始化;变量满足条件;变量变化规律){

if(boolean的值或表达式){

语句;

}

}

该结构表示,在for内,满足if条件时,执行语句。
例如:
package cn.java.lhb.syntax;

/**
 * 
 * @author lhb
 *
 */
public class ForDemo {
	public static void main(String[] args) {
		for (int i = 0; i <= 100; i++) {
			if (i % 2 == 0) {
				System.out.print(i + "\t");
			}
		}
	}
}
该程序输出结果为:0,2,4.....98,100

二:while循环语句(确切知道循环次数或者循环次数较少

(1)标准结构

变量初始化;

while(变量满足条件){

语句;

变量变化规律;

}

(2)基本结构

int i=0;

while(i<3){
System.out.print("Hoole Java");//语句

i++;
}

该程序表示,定义一个整型变量i,并且i初始化为1;满足i<3的情况下运行语句,运行结束后,i自增1。直至i<3条件不成立,跳出循环。

所以该程序为                    

    i=0;

执行语句。

    i=1;

执行语句。

    i=2;

执行语句。

跳出循环。
例如:
package cn.java.lhb.syntax;

/**
 * 
 * @author lhb
 *
 */
public class WhileDemo {

	public static void main(String[] args) {
		int i = 0;
		while (i <= 100) {
			System.out.print(i + "\t");
			i++;
		}
	}
}
该程序输出结果为:0,1,2,3.....99,100

(3)循环套用条件语句

变量初始化;

while(变量满足条件){

if(boolean的值或表达式){

语句;

}

变量变化规律;

}

该结构表示,在while内,满足if条件时,执行语句。
例如:
package cn.java.lhb.syntax;

/**
 * 
 * @author lhb
 *
 */
public class WhileDemo {

	public static void main(String[] args) {
		int i = 0;
		while (i <= 100) {
			if (i % 2 != 0) {
				System.out.print(i + "\t");
			}
			i++;
		}
	}
}
该程序输出结果为:0,2.4.....98,100

三:do......while循环语句(至少需要执行一次的循环)

(1)标准结构

变量初始化;

do{

语句;

变量变化规律;

}while(变量满足条件);

(2)基本结构

int i=0;

do{
System.out.print("Hoole Java");//语句

i++;
}while(i<3);

该程序表示,定义一个整型变量i,并且i初始化为1;执行语句,然后判断是否i<3,满足条件继续执行语句,运行结束后,i自增1。直至i<3条件不成立,跳出循环。

所以该程序为                

      i=0;

执行语句。

    i=1;

执行语句。

    i=2;

执行语句。

跳出循环。
例如:
package cn.java.lhb.syntax;

/**
 * 
 * @author lhb
 *
 */
public class WhileDemo {

	public static void main(String[] args) {
		int i = 0;
		do {
				System.out.print(i + "\t");
			i++;
		} while (i <= 100);
	}
}
该程序输出结果为:0,1,2,3.....99,100

(3)循环套用条件语句

变量初始化;

while(变量满足条件){

if(boolean的值或表达式){

语句;

}

变量变化规律;

}

该结构表示,在do.....while内,满足if条件时,执行语句。
例如:
package cn.java.lhb.syntax;

/**
 * 
 * @author lhb
 *
 */
public class WhileDemo {

	public static void main(String[] args) {
		int i = 0;
		do {
			if (i % 2 == 0) {
				System.out.print(i + "\t");
			}
			i++;
		} while (i <= 100);
	}
}
该程序输出结果为:0,2.4.....98,100

四:循环语句的应用:

穷举法是一种“笨办法”,通过程序运行所有的可能,找出需要找出的情况。
例如:
百元买百鸡:用一百元钱买一百只鸡。 已知公鸡5元/只,母鸡3元/只,小鸡1元/3只。
方法1:运用三重循环找出所有可能,用if条件语句判断并输出。
package cn.java.lhb.syntax;

/**
 * 
 * @author lhb
 *
 */
public class CenturyBuyChickenDemo {

	public static void main(String[] args) {
		for (int a = 0; a < 101; a++) {
			for (int b = 0; b < 101; b++) {
				for (int c = 0; c < 101; c++) {
					if ((a + b + c == 100) && (5 * a + 3 * b + c / 3 == 100)
							&& (c % 3 == 0)) {
						System.out.println("百元共买了" + a + "只公鸡," + b + "只母鸡,"
								+ c + "只小鸡。");
					}
				}
			}
		}
	}
}
方法2:运用两重循环找出所有可能,用if条件语句判断并输出。
package cn.java.lhb.syntax;

/**
 * 
 * @author lhb
 *
 */
public class CenturyBuyChickenDemo {

	public static void main(String[] args) {
		int x, y, z;
		for (x = 0; x <= 100; x++) {
			for (y = 0; y <= 100; y++) {
				z = 100 - x - y;
				if (5 * x + 3 * y + z / 3.0 == 100) {
					System.out.println("百元共买了" + x + "只公鸡," + y + "只母鸡," + z
							+ "只小鸡。");
				}
			}
		}

	}
}
方法1与方法2程序输出结果均为:
百元共买了0只公鸡,25只母鸡,75只小鸡。
百元共买了4只公鸡,18只母鸡,78只小鸡。
百元共买了8只公鸡,11只母鸡,81只小鸡。
百元共买了12只公鸡,4只母鸡,84只小鸡。





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值