Common logical structure exercises Java_Note

Source: Internet
Author: User

Print? Public class AppMain {

Public static void main (String [] args ){
AppMain appMain = new AppMain ();
AppMain. caculate ();
AppMain. test2 ();
AppMain. multiplicationTable ();
AppMain. addAdd ();
AppMain. addFactorial ();
}
/*
* Use the while loop to calculate the sum of the odd and even numbers within 100
*/
Void caculate (){
Int sumOdd = 0;
Int sumEven = 0;
For (int I = 0; I <100; I ++ ){
If (I % 2 = 0 ){
SumEven + = I;
} Else {
SumOdd + = I;
}
}
System. out. println ("sum of even numbers in 100:" + sumEven );
System. out. println ("sum of the odd numbers in 100:" + sumOdd );
}
/*
* Output the number of all vertices divided by 5 in 1000 in a cyclic structure, and output a maximum of three records per line.
*/
Void test2 (){
Int I = 0, j = 0;
While (I <1000 ){
If (0 = I % 5 ){
System. out. print (I + "\ t ");
J ++;
If (0 = j % 3 ){
System. out. println ("\ n ");
J = 0;
}
}
I ++;
}
}
/*
* Output the 9-9 multiplication table
*/
Void multiplicationTable (){
System. out. println ();
For (int I = 1; I <10; I ++ ){
For (int j = 1; j <= I; j ++ ){
System. out. print (j + "*" + I + "=" + (I * j <10? ("" + I * j): I * j) + "");
}
System. out. println ();
}
}
/*
* Request: 1 + (1 + 2) + (1 + 2 + 3) +... + (1 + 2 +... + 100)
*/
Void addAdd (){
 
Int sum = 0;
For (int I = 1; I <= 100; I ++ ){
Int tempSum = 0;
For (int j = 1; j <= I; j ++ ){
TempSum + = j;
}
Sum + = tempSum;
}
System. out. println (sum );
}
/*
* Request: 1! + 2! + 3! +... + 15!
*/
Void addFactorial (){
Long result = 0;
For (int I = 1; I <= 15; I ++ ){
Int temp = 1;
For (int j = 1; j <= I; j ++ ){
Temp * = j;
}
Result + = temp;
}
System. out. println (result );
}
}

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.