while语句的使用
package test;
/**
* This is the ninth cod.Names and comments should follow minfanphd@163.com.
*
* @author Mitchellemin@163.com
*/
public class WhileStatement {
/**
*
* The entrance of the program.
*
* @param args
* Not use now.
*
*/
public static void main(String args[]) {
whileStatementTest();
}// Of main
/**
*
* The sum not exceeding a given value.
*/
public static void whileStatementTest() {
int tempMax = 100;
int tempValue = 0;
int tempSum = 0;
//此时右侧直接赋值不需要int,但在之前进行矩阵运算的时候,右侧也是需要int[3][4]这样的,是为了书写美观么?
// Approach 1.
while (tempSum <= tempMax) {
tempValue++;
tempSum += tempValue;
System.out.println("tempValue = " + tempValue + ", tempSum = "
+ tempSum);
}// Of for while
//会一直在while中循环,直到不满足while条件
tempSum -= tempValue;
System.out.println("The sum not exceeding " + tempMax + " is: "
+ tempSum);
// Approach 2.
System.out.println("\r\n Alternative approach.");
tempValue = 0;
tempSum = 0;
while (true) {
//这里如何判定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 class WhileStatement