Java Syntax Basics for statement Practice _java

Source: Internet
Author: User
Control Statement--for Exercises
Nested application of the statement
Cumulative Sum, counter
Loop nesting
Nested application of a statement
Statement nesting form. In fact, there are statements in the statement. Various forms, no fixed format and routines.
1, print even
for (int x=1;x<=10;x++)
{if (x%2==1)
Continue
System.out.prinln ("x=" +x);
}
Add sum, counter
1, get the 1~10 and print.
Train of thought://1, defines variables for storing ever-changing and.
int sum = 0;
2, define the variable, and record the constantly changing number of added.
int x = 1;
3, define the cycle, repeat the process of addition.
Use while to reflect
while (x<=10)
{
sum = sum + x;
x + +;
}
System.out.println ("sum=" +sum);
Circular Note:
Be sure to identify which statements need to be involved in the loop and which do not need
Copy Code code as follows:

Class ForTest2
{
public static void Main (string[] args)
{
Use for to reflect.
int sum = 0;
for (int x=0; x<=10; x + +)
{
sum = x;
}
System.out.println ("For sum =" +sum);
}
}

A little summary of small examples: In fact, this is the accumulation of ideas.
Principle: The result of each change is recorded by a variable.
In the form of circulation, the cumulative action.
2, the number of multiples of 7 between 1~100. and print.
idea:
1, the first loop (traversal) of the 1~100 through the form of a loop.
2, in the process of traversal, define the conditions. operates only 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 the loop statement and select the For statement.
2, define the judgment in the loop. Just a multiple of 7. Use the IF statement. Conditions: Multiples of 7 x%7==0;
3, define the variable, which increases with the occurrence of a multiple of 7.
Copy Code code 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 a small sample:
This is the counter idea. Record the state change of the data through a variable. Maybe it's done through loops.
third, loop nesting.
1, print a rectangle.
Copy Code code 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 to change lines.
}
System.out.println ("-------------------");
}
}

****
****
****
For the Print rectangle summary: The number of lines outside the loop control. The inner loop controls the number of columns per row. That is, the number of elements in a row.
2, print a right-angled triangle, toe down.
Copy Code code as follows:

Class Forfordemo
{
public static void Main (string[] args)
{
/*
*****
****
***
**
*
Found that there are many rows in the graph, and there are many columns for each row.
You want to use nested loops. Principle: Image saying: Big snare small circle.
*/
int z = 5;
for (int x=0; x<5 x + +)//x<5: The outer loop controls the number of rows. 5 lines altogether.
{
for (int y=x; y<5; y++)
{
System.out.print ("*");
}
System.out.println ();
z++;
}
}
}

Here's a small summary of the example: there are many rows in the graph, and there are many columns for each row.
You want to use nested loops. Principle: Image saying: Big snare small circle.
3, printing triangle, Yang Hui's triangle, 99 multiplication table
/*
*
**
***
****
*****
1
12
123
1234
12345
99 Multiplication Table
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
*/
Copy Code code 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 ("----------------------");
/*

99 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 triangle, the Yang Hui's triangle, the 99 multiplication table obtains some not the rule law:
is not the law of Law:
ON the cusp, you can change the condition. Let the condition change with the outer loop.
With a pointed face, you can initialize the value so that the initialization changes with the outer loop.
4, Print diamond (◇) or pyramid
/*
----*
---* *
--* * *
-* * * *
* * * * *
* * * * *
-* * * *
--* * *
---* *
----*
*/
Copy Code code 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, Practice: 3000 meters long rope, reduce half a day. Ask how many days this rope will be less than 5 meters? Do not consider decimals.
Copy Code code 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, know that in the face of problems, first of all to clear what the problem is, and then whether they have ideas, and then convert the idea into Java can recognize the steps, and finally through the Java language can be realized.

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.