JAVA学习—while语句—2021.5.28

JAVA学习—while语句—2021.5.28

while语句:
只要指定条件为 true,循环就可以一直执行代码块。
语法:
while (条件)
{
需要执行的代码
}

do/while 循环
do
{
需要执行的代码
}
while (条件);
do/while 循环是 while 循环的变体。该循环会在检查条件是否为真之前执行一次代码块,然后如果条件为真的话,就会重复这个循环。该循环至少会执行一次,即使条件为 false 它也会执行一次

代码:

package a9;
/**
 * 
 * @author hengyuzuo
 *
 */
public class Whilestatement {
	/**
	 * ********************
	 * The entrance of the program.
	 * @param args Not used now.
	 * ********************
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		whileStatementTest();
	}// Of main

	/**
	 * ********************
	 * The sum not exceeding the given value
	 * ********************
	 */
	public static void whileStatementTest() {
		// TODO Auto-generated method stub
		int tempMax = 100;
		int tempValue = 0;
		int tempSum = 0;
		
		//method1
		while (tempSum <= tempMax) {
			tempValue++;
			tempSum += tempValue;
			
			System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
		}// Of while
		
		tempSum -= tempValue; 
		
		System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
		
		//method2
		System.out.println("Now let's try Another method.");
		tempValue = 0;
		tempSum = 0;
		while (true) {
			tempValue++;
			tempSum += tempValue;
			
			System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
			
			if (tempMax < tempSum) {
				break;
			}// Of if
		}// Of while
		tempSum -= tempValue;
		
		System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
	}// Of whileStatementTest
}// Of whileStatement

运行结果:

tempValue = 1, tempSum = 1
tempValue = 2, tempSum = 3
tempValue = 3, tempSum = 6
tempValue = 4, tempSum = 10
tempValue = 5, tempSum = 15
tempValue = 6, tempSum = 21
tempValue = 7, tempSum = 28
tempValue = 8, tempSum = 36
tempValue = 9, tempSum = 45
tempValue = 10, tempSum = 55
tempValue = 11, tempSum = 66
tempValue = 12, tempSum = 78
tempValue = 13, tempSum = 91
tempValue = 14, tempSum = 105
The sum not exceeding 100 is: 91
Now let's try Another method.
tempValue = 1, tempSum = 1
tempValue = 2, tempSum = 3
tempValue = 3, tempSum = 6
tempValue = 4, tempSum = 10
tempValue = 5, tempSum = 15
tempValue = 6, tempSum = 21
tempValue = 7, tempSum = 28
tempValue = 8, tempSum = 36
tempValue = 9, tempSum = 45
tempValue = 10, tempSum = 55
tempValue = 11, tempSum = 66
tempValue = 12, tempSum = 78
tempValue = 13, tempSum = 91
tempValue = 14, tempSum = 105
The sum not exceeding 100 is: 91

2.1-100的奇数和

代码

package test1;
/**
 * 
 * @author hengyuzuo
 *
 */
public class TestDemo3 {
	
	public static void mian (String[] args) {
		int sum = 0;
		int i = 1;
		while(i <= 100) {
			if( i % 2 == 1) {
				sum += i;
			}// Of if
			i++;
		}// Of while
	System.out.println("奇数和为:" + sum);
	}// Of main
	
}// Of class TestDemo3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值