Basic for statement exercises in Java syntax

  • 2020-04-01 02:09:24
  • OfStack

Control statement - for exercise
Nested application of statements
Summation, counter
A nested loop
One, the nested application of statements
Nested statement form. There are statements in statements. There are various forms and no fixed format or routine.
1. Print even Numbers
For (int x = 1; X< = 10; X++)
{  If (x % 2 = = 1)
The continue;
  System. The out. Prinln (+ x "x =");
}
Two, sum up, counter
1. Get the sum of 1~10 and print it.
//1, define variables to store the constantly changing sum.
            Int sum = 0;
            //2, define variables to keep track of the changing Numbers added.
            Int x = 1;
            //3, define the loop and repeat the process of addition.
                  Use while
            While (x< = 10)
            {
                    Sum is equal to sum plus x;
                    X++;
            }
            System. The out. Println (" sum = "+ sum);
Circular note:
Be sure to identify which statements need to participate in the loop and which ones do not

class  ForTest2
{
 public static void main(String[] args) 
 {
  //Let's use for.
  int sum = 0;
  for(int x=0; x<=10; x++)
  {
   sum += x;
  }
  System.out.println("for sum = "+sum);
 }
}

A small summary of a small example: this is the idea of accumulation.
Principle: the result of each change is recorded by the variable.
Through the form of circulation, carry on the accumulation action.
2. The number of multiples of 7 between 1 and 100. And print.
Ideas:
1. Loop (traversal) through the form of loop from 1 to 100.
2. During the traversal, define the condition. We only operate on multiples of 7.
3, because the multiple of 7 is uncertain, as long as the conditions are met, the number of the change is recorded by a variable.
Steps:
1. Define the loop statement and select the for statement.
2. Define the judgment in the loop. As long as it's a multiple of 7. Use the if statement. Condition: multiple of 7 x%7==0;
3. Define the variable, which increases with the occurrence of multiples of 7.

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 example:
That's the counter idea. A variable is used to record changes in the state of the data. Maybe through a loop.
Loop nesting.
1. Print a rectangle.

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();//There is only one function: line feed.
  }
  System.out.println("-------------------");
 }
}

* * * *
* * * *
* * * *
For print rectangle summary: number of lines controlled by outer loop. The inner loop controls the number of columns per row. That's the number of elements in a row.
2. Print a right triangle, toe down.

class ForForDemo 
{
 public static void main(String[] args) 
 {
  
  //int z = 5;
  for (int x=0; x<5 ;x++ )//X<5: because the outer loop controls the number of rows. There are five rows.
  {
   for (int y=x; y<5 ;y++)
   {
    System.out.print("*");
   }
   System.out.println();
   //z++;
  }
 }
}

A small summary from this example: find that the graph has many rows, and each row has many columns.
Use nested loops. Principle: image saying: big trap small circle.
3. Print positive triangle, Yang hui triangle and multiplication table


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("----------------------");
  
  for (int x=1; x<=5; x++)
  { 
   for (int y=1; y<=x;y++ )
   {
    System.out.print(y);
   }
   System.out.println();
  }
  System.out.println("----------------------");
  
  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();
  }
 }
}

According to the regular triangle, Yang hui triangle and the multiplication table, we can get some irregular laws:
A law which is not a law:
            It's pointed up, you can change the conditions. Let the conditions change with the outer loop.
            Point down, you can initialize the value and let the initialization change with the outer loop.
4, printing diamond ( To cardiac ) or known as the pyramid


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: 3000 meters of rope, a half a day. How many days is this rope going to be less than 5 meters? I don't care about decimals.

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 exercise, know in the face of problems, first to clarify what the problem is, then whether they have ideas, and then the ideas into Java can recognize the steps, and finally through the Java language to achieve.

Related articles: