Java syntax basics-for statement exercises

Source: Internet
Author: User

The following is a detailed description of the for statement in the Java syntax. If you need it, refer to it.

Control statement-for exercise
Nested application of statements
Accumulate sum, counter
Loop nesting
I. nested application of statements
Statement nesting form. In fact, there are still statements in the statement. There are various forms without fixed formats and routines.
1. Print even numbers
For (int x = 1; x <= 10; x ++)
{If (x % 2 = 1)
Continue;
System. out. prinln ("x =" + x );
}
Ii. accumulate sum and counter
1. Obtain 1 ~ And print.
Idea: // 1. Define variables to store ever-changing sums.
Int sum = 0;
// 2. Define the variable and record the variable number.
Int x = 1;
// 3. Define the loop and repeat the addition process.
Use while to reflect
While (x <= 10)
{
Sum = sum + x;
X ++;
}
System. out. println ("sum =" + sum );
Loop note:
Be sure to specify which statements need to participate in the loop and which do not need

Copy codeThe Code is as follows:
Class ForTest2
{
Public static void main (String [] args)
{
// Use.
Int sum = 0;
For (int x = 0; x <= 10; x ++)
{
Sum + = x;
}
System. out. println ("for sum =" + sum );
}
}


Summary of the small example: in fact, this is the accumulation idea.
Principle: records the results of each change through variables.
Accumulate actions in the form of loops.
2. 1 ~ The number of multiples of 7 between 100. And print.
Ideas:
1, first to 1 ~ 100.
2. define conditions during traversal. Only operate on multiples of 7.
3. Because the multiples of 7 are uncertain, as long as the conditions are met, a variable is used to record the number of changes.
Steps:
1. Define a loop statement and select for statement.
2. Define the judgment in the loop. As long as it is a multiple of 7. Use the if statement. Condition: x % 7 = 0;
3. Define the variable. The variable increases with the appearance of a multiple of 7.

Copy codeThe Code is as follows:
Class ForTest3
{
Public static void main (String [] args)
{
Int count = 0;
For (int x = 1; x <= 100; x ++)
{
If (x % 7 = 0)
// System. out. println ("x =" + x );
Count ++;
}
System. out. println ("count =" + count );
}
}


Summary of small examples:
This is the counter idea. A variable is used to record the status changes of the data. It may be done through loops.
3. nested loops.
1. Print a rectangle.

Copy codeThe Code is as follows:
Class ForForDemo
{
Public static void main (String [] args)
{
/*
****
****
****
*/
For (int x = 0; x <3; x ++ )//
{
For (int y = 0; y <4; y ++)
{
System. out. print ("*");
}
System. out. println (); // only one function is line feed.
}
System. out. println ("-------------------");
}
}


****
****
****
Summary of printed rectangles: number of rows controlled by external loops. The number of columns in each row is controlled cyclically. That is, the number of elements in a row.
2. Print a right triangle with the toe facing down.

Copy codeThe Code is as follows:
Class ForForDemo
{
Public static void main (String [] args)
{
/*
*****
****
***
**
*
It is found that there are many rows in the graph, and each row has many columns.
Use nested loops. Principle: Image statement: big traps and small circles.
*/
// Int z = 5;
For (int x = 0; x <5; x ++) // x <5: the number of rows controlled by the External Loop. Five rows in total.
{
For (int y = x; y <5; y ++)
{
System. out. print ("*");
}
System. out. println ();
// Z ++;
}
}
}


Through this example, we can conclude that there are many rows in the graph, and each row has many columns.
Use nested loops. Principle: Image statement: big traps and small circles.
3. Print the positive triangle, Yang Hui triangle, and 9-9 multiplication table
/*
*
**
***
****
*****
1
12
123
1234
12345
9-9 multiplication table
1*1 = 1
1*2 = 2 2*2 = 4
1*3 = 3 2*3 = 6 3*3 = 9
*/

Copy codeThe Code is as follows:
Class ForForTest
{
Public static void main (String [] args)
{
/*

*
**
***
****
*****

*/
For (int x = 0; x <5; x ++)
{
For (int y = 0; y <= x; y ++)
{
System. out. print ("*");
}
System. out. println ();
}
System. out. println ("----------------------");
/*

1
12
123
1234
12345
*/
For (int x = 1; x <= 5; x ++)
{
For (int y = 1; y <= x; y ++)
{
System. out. print (y );
}
System. out. println ();
}
System. out. println ("----------------------");
/*

9-9 multiplication table
1*1 = 1
1*2 = 2 2*2 = 4
1*3 = 3 2*3 = 6 3*3 = 9

*/
For (int x = 1; x <= 9; x ++)
{
For (int y = 1; y <= x; y ++)
{
System. out. print (y + "*" + x + "=" + y * x + "t ");
}
System. out. println ();
}
}
}


Through the positive triangle, the Yang Hui triangle, and the 9-9 multiplication table, we can find some irregular rules:
This is not a regular law:
Tip up, you can change the condition. Let the condition change with the External Loop.
The value can be initialized with a point down, so that the initialization changes with the External Loop.
4. Print a diamond (◇) or a pyramid.
/*
----*
---**
--***
-****
*****
*****
-****
--***
---**
----*
*/

Copy codeThe Code is as follows:
Class ForForTest2
{
Public static void main (String [] args)
{
For (int x = 0; x <5; x ++)
{
For (int y = x + 1; y <5; y ++)
{
System. out. print ("");
}
For (int z = 0; z <= x; z ++)
{
System. out. print ("*");
}
System. out. println ();
}
}
}


5. Exercise: A 3000-meter-long rope, halved every day. How many days will the rope be less than 5 meters? Decimal places are not considered.

Copy codeThe Code is as follows:
Class ForTest4
{
Public static void main (String [] args)
{
Int day = 0;
For (int x = 3000; x> = 5; x/= 2)
{
Day ++;
}
System. out. println ("day =" + day );
}
}


Through the above exercises, we know what the problem is first when we encounter a problem, and then whether we have ideas, and then convert the ideas into steps that java can recognize, finally, use the java language.

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.