Cycle
Calculates the value of the 1+3+5+7+...+99 with a for loop and outputs the result of the calculation.
Calculate 1 with a For loop! +2! +3! +...+10! Value and outputs the result of the calculation.
The first 5 numbers within the output 1~100 can be divisible by 3.
Outputs the prime number within the 101~200.
//calculates the value of the 1+3+5+7+...+99 with a for loop and outputs the result of the calculation. Public classTest {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method Stub intAdd = 0; for(inti = 1; I <= 99; i + = 2) {Add+=i; } System.out.println ("1+3+5+7+...+99 =" +Add); }}
Printed results:
1+3+5+7+...+99 = 2500
1 //calculate 1 with a For loop! +2! +3! +...+10! Value and outputs the result of the calculation. 2 Public classTest {3 /**4 * @paramargs5 */6 Public Static voidMain (string[] args) {7 //TODO auto-generated Method Stub8 Longresult = 0;9 LongF=1;Ten for(inti = 1; I <= 10; i++) { Onef=f*i; Aresult+=F; - - } theSYSTEM.OUT.PRINTLN ("1! +2! +3! +...+10! = "+result); - } -}
Printed results:
1! +2! +3! +...+10! = 4037913
1 //the first 5 numbers within the output 1~100 can be divisible by 3. 2 Public classTest2 {3 4 /**5 * @paramargs6 */7 Public Static voidMain (string[] args) {8 //TODO auto-generated Method Stub9 intnum = 0, i = 1;Ten while(I <= 100){ One if(i% 3 = = 0){ ASYSTEM.OUT.PRINTLN (i + "")); -num++; - } the if(num = = 5) - Break; -i++; - } + - } + A}
Printing results:
3
6
9
12
15
1 //outputs the prime number within the 101~200. 2 Public classTest2 {3 4 /**5 * @paramargs6 */7 Public Static voidMain (string[] args) {8 //TODO auto-generated Method Stub9 for(inti = 101; I < 200; i + = 2) {Ten Booleanf =true; One for(intj = 2; J < I; J + +) { A if(i% j = 0) { -f =false; - Break; the } - } - if(!f) - Continue; +System.out.print ("" +i); - } + } A}
Java Basic syntax