Java three methods of cyclic summation, java three methods of summation
Note: The sum of 100 is 5050.
Common for loop:
Public class HundredSum {public static void main (String [] args) {int x = 0; for (int I = 1; I <= 100; I ++) {x = x + I; // x + = I;} System. out. print (x );}}
While loop:
Public class HundredSum {public static void main (String [] args) {int x = 0; int I; while (I <= 100) {x = x + I; // x + = I; I ++;} System. out. print (x );}}
Do-while loop:
Public class HundredSum {public static void main (String [] args) {int I = 0, x = 0; do {x = x + I; // x + = I; I ++;} while (I <= 100); // loop the do statement block first, and then execute while. If the while condition is not met, the System jumps out of the loop. out. print (x );}}
The above are three common methods for summation. Thank you for your support for the help house.