其他分享
首页 > 其他分享> > Day9 while语句

Day9 while语句

作者:互联网

今天的不难

 1 public class WhileStatement {
 2     
 3     /**
 4      * The entrance of the program
 5      * @param args
 6      */
 7     public static void main(String args[]) {
 8         whileStatementTest();
 9     }// Of main
10 
11     /**
12      * The sum not exceeding a given value
13      */
14     private static void whileStatementTest() {
15         int tempMax = 100;
16         int tempValue = 0;
17         int tempSum = 0;
18 
19         // Approach 1.
20         while (tempSum < tempMax) {
21             tempValue++;
22             tempSum += tempValue;
23             System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
24         }// Of while
25         tempSum -= tempValue;
26 
27         System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
28 
29         // Approach 2.
30         System.out.println("\r\nAlternative approach.");
31         tempValue = 0;
32         tempSum = 0;
33         while (true) {
34             tempValue++;
35             tempSum += tempValue;
36             System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
37 
38             if (tempMax < tempSum) {
39                 break;
40             }// Of if 
41         }// Of while
42         tempSum -= tempValue;
43 
44         System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
45     }// Of WhileStatementTest
46 
47 }

 

标签:语句,Day9,System,while,tempValue,tempSum,tempMax,out
来源: https://www.cnblogs.com/f1uency/p/16187168.html