1.while语句 格式一: while(条件判断句){ 循环体语句; 条件控制语句; } 格式二: do{ 循环体语句; 条件控制语句; }while(条件判断句) 2.应用—累加 package com.one; public class WhileStatement { public static void main(String args[]) { whileStatementTest(); }// of main public static void whileStatementTest() { int tempMax = 100; int tempValue = 0; int tempSum = 0; // Approach 1. //退出循环的条件,当sum的值大于max的值 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); // Approach 2. //循环会一直执行下去,但是当sum的值大于max,就会执行break语句,循环会强制退出 System.out.println("\r\nAlternative approach."); 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 class 运行结果