JAVA hobbled self-taught Japanese sentence

Source: Internet
Author: User

Judging structure

if (conditional expression) {EXECUTE statement} one level of judgment

When the result of a conditional expression is true, the statement inside the curly braces is executed, and the result is false when the statement inside the curly brace is not executed, but skipped. For example int x = 9;

if (x>3) {System.out.println ("execute");}

int x = 2;

if (x>3) {System.out.println ("execute");} expression result is false skip back code and Continue execution

if (conditional expression) {Execute code 1} else{execute code 2}

If the result inside the conditional expression is true, execute code 1 to run. If the result is False, execute code 2 runs.

Second-level judgment

Class Ifdemo
{
public static void Main (string[] args)
{
int x = 8;
if (x>9)
{
System.out.println ("Hello 1");
} else
{
System.out.println ("Hello 2");
}
}
}

if (conditional expression) {Execute code 1} else if (conditional expression 2) {Execute code 2} else if (conditional expression 3) {Execute Code 3}.........................else {}   multilayer judgment

class  ifdemo
{
    public static void Main (string[] args)
    {
&N bsp;           int x = 8;
            if (x>9)
             {
                 System.out.println ("Hello 1");
           } else if (x>7)
                 {
                  System.out.println ("Hello 2");
                }  else if (x>3)

{
System.out.println ("Hello 3");
} else

{System.out.println ("over");}
}
}

Multi-layered judgment automatically executes the following code and ends the entire statement as long as any one of these conditions is satisfied

Select Structure Switch

Format

switch (the value that participates in the selection)//The value inside can only be a byte short int char four types of
{
Case 1:
Code 1;
Break

Case 2:
Code 2;
Break

Case 3:
Code 3;
Break
Case 4:
Code 4;
Break

..........................................
Case N:
Code N;
Break

Default
Code
Break

}

Example: Judging by the given month value which season it belongs to

Thinking: Only 12 months a year 3,4,5 spring 6,7,8 summer 9,10,11 autumn 12,1,2 Winter

There is a specific value and the number is not large and the value is byte type data, so we choose the switch to select the structure of the statement to complete

Class Switchdemo
{
    public static void Main (string[] args)
    {
&nbs p;       byte x = 2;
        switch (x)
        {
            Case 3:
                 System.out.println ("This is spring Oh");
            break;

Case 4:
System.out.println ("This is spring Oh");
Break
Case 5:
System.out.println ("This is spring Oh");
Break

Case 6:
System.out.println ("This is Summer Oh");
Break

Case 7:
System.out.println ("This is Summer Oh");
Break
Case 8:
System.out.println ("This is Summer Oh");
Break

Case 9:
System.out.println ("This is Autumn Oh");
Break

Case 10:
System.out.println ("This is Autumn Oh");
Break
Case 11:
System.out.println ("This is Autumn Oh");
Break

Case 12:
System.out.println ("This is Winter Oh");
Break

Case 2:
System.out.println ("This is Winter Oh");
Break
Case 1:
System.out.println ("This is Winter Oh");
Break

Default
SYSTEM.OUT.PRINTLN ("Input incorrect value, no corresponding month exists");
Break
}
}

}

If the switch has the same execution code and only compares the selected values, you can merge the same code's selection values to remove those break

Class Switchdemo
{
public static void Main (string[] args)
{
byte x = 2;
Switch (x)
{
Case 3:
Case 4:
Case 5:
System.out.println ("This is spring Oh");
Break

Case 6:
Case 7:
Case 8:
System.out.println ("This is Summer Oh");
Break

Case 9:
Case 10:
Case 11:
System.out.println ("This is Autumn Oh");
Break

Case 12:
Case 2:
Case 1:
System.out.println ("This is Winter Oh");
Break

Default
SYSTEM.OUT.PRINTLN ("Input incorrect value, no corresponding month exists");
Break
}
}

Loop statement while

Format

Initialize an expression

while (conditional expression) {EXECUTE statement}

Class Whiledemo
{
public static void Main (string[] args)
{
byte x = 9;
while (X<20)
{
System.out.println ("Hello world!" + "x =" +x);
x + +;
}
}
}

Loop statement for

Class Whiledemo
{
public static void Main (string[] args)
{
for (byte y =9;y<20; y++)
{
System.out.println ("Hello world!" + "y =" +y);
}
}
}

The difference between the while and for Two loop bodies two loop bodies can be used to convert the while initialization variable to the outside of the loop body after the end of the loop in memory or stored in the variable for loop control loop body increment of the scope of the variable is inside the for, the loop after the variable also with It was released. Relatively save a little bit of memory, if the loop body after the end of the subsequent program still need to apply that variable to use while, if the control loop body variable is only a simple control loop then use for.

Counter idea

Prints the number of 9 integer multiples of 1-100 to the console

Analysis: 9 of the number of integer multiples of the characteristics/9 after the remainder is 0 if (x/9==0) if the expression inside the result is true then X is the number of integers of 9

From 1-100 to find out all the number of satisfied conditions need one to use the above method to find that is to repeat the above action 100 times so to use the loop body to do. for (byte x=1;x<=100;x++)

{

if (x/9==0);

}

Since the number of matching requirements is selected, we just need to see how many of them are in total, so it's good to count each time you pick it up. Define a variable count to count

for (byte x=1;x<=100;x++)

{

if (x/9==0);

count++;

}

For nesting

Print a rectangle with an * number

Class Forfortest
{
public static void Main (string[] args)
{
for (byte x=0;x<4; x + +)
{
for (byte y=0;y<6; y++)
{
System.out.print ("*");
}
System.out.println ();
}
}
}

Run results

Analysis:

for (byte y=0;y<6; y++)
{
System.out.print ("*");
}
The inner for Loop control column changes the number of stars in each row

The outer for loop controls the line changes that print a few lines of stars

Change 1 print a SMB down star

Class Forfortest
{
public static void Main (string[] args)
{
for (byte x=0;x<4; x + +)
{
for (byte y=x;y<4; y++)
{
System.out.print ("*");
}
System.out.println ();
}
}
}

Analysis: Print a few lines of stars do not change or those lines, so the outer for no change, only change the inner layer can be

JAVA hobbled self-taught Japanese sentence

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.