"Java Syntax example 2~15 chapter"

Source: Internet
Author: User
Tags array length

Chapter II

1. Data type variable name = value;

Eg:

Double score = 15.65;

String name = "Zhang San";

Char sex = "male";

    1. Data type Strong turn

Data type variable name = (data type) value;

Example:

Double num=15.75;

int sum = (int) num;

Results: sum=15;

Chapter III 1. If selection structure

Syntax: if (condition) {

code block//After the condition is established, it can be a statement or a set of statements

}

Example:

If (7>5) {

System.out.println ("true");

}

    1. IF--ELSE selection Structure

Syntax: if (condition) {

Code Block 1

}else{

Code Block 2

}

Example:

if (chengji>98) {

System. out. println ("Teacher said: Yes, reward a mp4");

}Else {

System. out. println ("teacher says: Code penalty!!!" ");

}

3. multiple if selection structure

Syntax: if (condition 1) {

Code Block 1

}else if (condition 2) {

Code Block 2

}else{

Code block 3

}

Example:

if (qian>=500) {

System. out. println ("Great, I can buy a Cadillac");

}Else if (qian>=100) {

System. out. println ("Also, I can sell a Passat");

}Else if (qian>=10) {

System. out. println ("I can buy an Austrian extension");

}Else if (qian>=5) {

System. out. println ("I can buy a Elantra");

}Else{

System. out. println ("It seems that I can buy a Czech own");

}

4. nested IF selection structure

Grammar:

if (condition 1) {

if (condition 2) {

Code Block 1

}else{

Code Block 2

}

}else{

Code block 3

}

Example:

if (chengji<=10) {

if (Xingbie.equals ("male")) {

System. out. println ("Enter the men's group final");

}Else if(xingbie.equals ("female")) {

System. out. println ("Enter the women's group Finals");

}

}Else{

System. out. println ("obsolete!!!!");

}

Fourth Chapter1. Switch Selection structure

Syntax: switch (condition) {

CASE constant 1:

code block 1;

Break

Case constant 2:

code block 2;

Break

......

Default

code block N;

Break

}

Example:

Switch (MINGCI) {

Case 1:

System. out. println ("Attend the one-month summer camp organized by MIT");

break;

Case 2:

System. out. println ("Reward hp notebook PC");

break;

Case 3:

System. out. println ("Reward mobile HDD One");

break;

default:

System. out. println ("no Rewards");

break;

}

Fifth Chapter1. While loop structure

Syntax: while (loop condition) {

Looping operations

}

Example:

int i = 1;

while (i<=100) {

System.out.println ("Struggle hard");

i = i + 1;

}

2. Do-while Cycle

do{

Looping operations

}while (cyclic operation);

Example:

int i = 1;

do{

System.out.println ("Fight hard!")

i++;

}while (i<=100);

Sixth Chapter1. For Loop

Grammar:

for (expression 1; expression 2; expression 3) {

Loop body

}

Example:

for (I=0, j=num;i<=num;i++,j--) {

System. out. println (i+ "+" +j+ "=" + (i+j));

}

    1. Use of break

Example:

for (int i =0;i<5;i++) {

System. out. println ("Please enter" + (i + 1) + "Gate score:");

Score = Input.nextint ();

if (score<0) {

Isnegative = true;

break;

}

    1. Use of Continue

Example:

for (int i = 0;i<totle;i++) {

System. out. println ("Please enter" + (i+1) + "Student's score:");

Score =input.nextint ();

if (score<80) {

continue;

}

Eighth Chapter

1. Declaring an array

Grammar:

data type [] array name; or data array name [];

Example:

Int[] Scres;

2, allocating space

Grammar:

Array name = new data type [array length];

Example:

Scores = new INT[10];

Integrated approach:

data type [] Array name = new data type [array length];

Example:

int scores[] = new int[10];//stores 10 data points

Watch out! Once the size of the array is declared, it cannot be modified. That is, the length of the array is fixed,

3. Assigning values

After allocating space, you can store the data in the array, and each element in the array is accessed by subscript.

Grammar:

Array name [subscript value];

Example:

To store data in an scores array

Scores[0] = 10;

Grammar:

data type [] Array name ={value 1, value 2, value 3,......... Value n};

Example:

int [] scores = {30.20.10,60.78};

Can also be written as:

Int[] scores = new int[]{30,20,10,60,78};

4. Array Sorting

Syntax: Arrays.sort (array name);

    1. To find the maximum value of an array

Example:

max = Scores[0];
for (int i = 1;i<scores.length;i++) {
if (Scores[i]>max) {
max = Scores[i];
}

    1. inserting elements

Example:

for (int i =0;i<list.length;i++) {
if (Num>list[i]) {
index = i;
Break
}
}
Element move back
for (int j = list.length-1;j>index;j--) {
List[j]= List[j-1];//index The element that starts with the subscript moves back one position

Nineth Chapter1. Double cycle Structure

1. Syntax://while and while loop nesting

while (loop condition 1) {

Circular Operation 1

while (loop condition 2) {

Circular Operation 2

}

}

Example:

Int i=0;

Int j=0;

while (i<3) {

while (j<3) {

System.out.println ("Fight hard!");

j + +;

}

i++;

}

2.//do-while and Do-while loop nesting

bo=

Circular Operation 1

bo=

Circular Operation 2

}while (cycle condition 2);

}while (cycle condition 1)

Example:

Int i=0;

Int j=0

do{

do{

System.out.println ("Fight hard!");

j + +;

}while (j<3)

i++;

}while (i<3)

3.//for and for loop nesting

for (loop condition 1) {

Circular Operation 1

for (loop condition 2) {

Circular Operation 2

}

}

Example:

for (int i = 0; i < average.length; i++) {

sum=0.0;

System. out. println ("Please enter the" + (i+1) + "Grade of the Class");

for (int j = 0; J < Score.length; J + +) {

System. out. println ("First" + (J+1) + "scores of participants");

Score[j]=input.nextint ();

SUM+=SCORE[J];

}

Average[i]=sum/score.length;

System. The average score of println ("i+1" + "class entry Academy" is "+average[i]+" \ n ");

}

4.//while with For loop nesting

while (loop condition 1) {

Circular Operation 1

for (loop condition 2) {

Circular Operation 2

}

}

Example:

for (int i=1;i<num;i++{
while (B[0]!=a[i]) {
B[1]=a[i];
count++;
Break
find = true;

}
if (find)
Break
}

11th Chapter1. class templates for Java

Grammar:

public class < class name > {

}

Example:

public class School {

String Schoolname;

int classnumber;

int labnumber;

    1. Method declaration

Grammar:

Access modifier return value type method name () {

Method body

}

Example:

public void Showcenter () {

SYSTEM.OUT.PRINTLN (schoolname+ "Training center \ n" + "equipped with:" +classnumber+ "classroom" +labnumber+ "machine");

}

3. How to create and use objects

Syntax for creating objects:

Class Name Object name = new class name ();

Example:

School Center = new School ();

The name of the object. Properties//referencing properties of an object

The name of the object. Method Name//method of referencing object

Example:

center.name = "Beijing Center"; Assigning a value to the Name property

Center.showcenter (); Call the Showcenter () method

12th Chapter

1. How to define a class method

Grammar:

Public return value type method name () {

The body of the method

}

Grammar:

return expression;

Example:

Public String robball{

String ball = "one";

return ball;

14th Chapter

1. Defining the Parameter method

Syntax:< access modifier > Return value type < method name > (< parameter list >) {

Method body

}

Example: public void Show (String name,int age) {

}

15th Chapter

String name= "Zhang San";

1. Get the length of a string

Syntax: Length ();

Example:

Name.length ();

2. Comparing strings

Syntax: Equals ();

Example:

Name.rquals ("Zhang San")

3. Connection string

Syntax: Concat ();

Example:

Name.concat ("18 years old");

4. Extracting a String

Syntax: substring ();

Example:

Name.substring (0); Extract No. 0 bit

5. Searching for strings

Syntax: IndexOf ();

Example:

Name.indexof (1); Returns the value of the 1th bit

6. Splitting a string

Syntax: Split (String separator,int limit);

Example:

String ci = "changting outside the ancient road edge";

Chai =ci.split (""); Split by Space

7. Remove the leading and trailing spaces

Syntax: Trim ();

Example:

Name.trim ();

StringBuffer

8. Convert to String type

Syntax: toString ();

Example:

StringBuffer a =new stringbuffer ("ABCDEFG");

A.tostring ();

9. Connection string

Syntax: Append ();

Example:

StringBuffer a =new stringbuffer ("ABCDEFG");

A.append ("Hijk");

10. Inserting a string

Syntax: Insert ();

Example:

StringBuffer a =new stringbuffer ("ABCDEFG");

for (int i=a.length () -3;i>0;i=i-3) {

A.insert (i, ', ');

}

"Java Syntax example 2~15 chapter"

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.