For Loop, for Loop

Source: Internet
Author: User

For Loop, for Loop

Print the 9-9 multiplication table using nested for Loop

1*1 = 1

1*2 = 2 2*2 = 4

.........

1*9 = 1 2*9 = 18 ...... 9*9 = 81

 

Public class For_03 {

Public static void main (String [] args ){

For (int I = 1; I <= 9; ++ I ){
For (int j = 1; j <= I; ++ j ){
System. out. print (j + "*" + I + "=" + (I * j) + "");
}
System. out. println ();
}
}
}

(Operating principle) Comments: When I = 1, I <= 9, enter the for loop below; j = 1, j <= I, if the condition is met, the output is (j + "*" + I + "=" + (I * j) + ""), that is, the output is 1*1 = 1, and a space is added, after the output, + j is returned. At this time, + j = 2, judge whether 2 is <= I (1), 2> 1 does not meet the condition, so no j * I is output, output line feed. Enter ++ I, ++ I is equal to 2, 2 <= 9 and meet the conditions. Enter the for loop below, j = 1, j <= I (2 ), output 1x2 = 2 in compliance with the condition, enter ++ j, ++ j = <= 2 in compliance with the condition, output 2x2 = 4, then, + + j = 3> 2. If the condition is not met, a line feed is output, and so on. When I = 9, j = 9, j <= I ends.

Running effect:

 

 

 

1 ~ 100 sum of all odd and even numbers.

 

Public class jiou {

Public static void main (String [] args ){

Int j = 0; // declare a variable j with int and assign a value of 0. The variable declaration involves three steps: dividing the data type into memory space, naming, and assigning values.

For (int I = 1; I <= 100; I + = 2) {// here is the for loop, declare a variable I with the int data type, and assign a value of 1, so that the maximum I value cannot exceed 100, each cycle I adds itself to 2

// Three elements of a loop: Initial Value (I = 1, indicating that I is output from the beginning)

// Termination condition (that is, if I <100, exceeds 100, the cycle ends)

// Step size: (I + = 2. Every cycle I is added with 2, so the step size is 2)

J + = I;

}

System. out. println ("the odd number of 1-and" yes :");

System. out. println (j );

// Output the variable j. Set 1 + 3 = 4, 4 + 5 = 9 .... display, 9, 16 ..... that is, the value overwritten by each addition is always added to the last value of I = 99, that is, the odd number and 2500 in 1-

 

Int o = 0;
For (int I = 0; I <= 100; I + = 2 ){
O + = I;

}
System. out. println ("the even number of 1-and the sum is :");
System. out. println (o); // Same principle as above

Running effect:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.