# # #基础语法 (3) # # #
1. Circular statement: For/while/do...while
1) for Loop
Format: for (loop variable initialization; loop variable condition; cyclic invariant change) {
Looping operations
}
Execution process:
<1> executes the loop variable initialization section, sets the loop initialization state, which executes only once in the loop
<2> loop condition judgment, true to execute loop inside code, false to exit loop directly
<3> perform cyclic variable changes to change the value of the loop variable for the next conditional judgment
<4> executes the loop sequentially until the condition reaches the statement is false, exits the loop
(Note: There is no need to add semicolons where there are curly braces, where there is no need for parentheses)
Example: Summing, finding the sum of 1~100
2) While loop
Format: while (judging condition) {
Looping operations
}
Execution process:
<1> determine if the condition behind the while is established
<2> When set up, perform the loop operation until the condition is not set to stop
Example: sequentially input 1-5
3) Do...while Cycle
Format: do{
Looping operations
}while (Judging condition)
Execution process:
<1> perform a loop operation first and then determine if the loop condition is true
<2> If set up continues execution until no immediate end loop
Example: Calculate an even number within 50 and
4) Looping The nesting
Example: Typing 9x9 multiplication table
ps:continue: Stop the current loop into the next loop, break: End Loop
2. Arrays: Arrays can be understood as a huge box in which multiple types of data can be stored sequentially.
Such as
Where elements can be accessed by subscript
The subscript of an array starts at 0 and is here score[0] = 76 ... SCORE[3] = 87
Initialization of the array:
1) Dynamic initialization: Specifies the length of the array to which the system assigns values to the elements in the array
(The format is: data type [] Array name = new array type [specified length])
2) static initialization: Specifies the value of the element in the array, specified by the system to the length of the array
Working with the data in the array: we can manipulate and manipulate the array after the assignment
We can directly create an array, which will declare an array, allocate space, and assign a merge to complete
Example: int[] sc = new int[]{5,15,25,35,48,55,38}
Array: Length property: Get array length
Example: input a set of data, and output the array
3. Method: The method is used to solve a kind of problem code of the orderly combination, that is, a function module
Syntax: Access modifier return value type method name (parameter list) {
Method body
}
1) Access modifier: The method allows access to the scope of the permission, can be public, protected, private or even omitted, where public means that the method can be called by any other code, the use of several other modifiers in the later chapters will explain in detail the drop
2) return value type: The type of the method return value, if the method does not return any value, the return value type is specified as void, if the method has a return value, you need to specify the type of the return value, and return the value in the method body using the return statement
3) Method Name: The name of the method defined, must use a valid identifier
4) parameter list: Parameter list passed to method, parameter can have multiple, multiple parameters separated by commas, each parameter consists of parameter type and parameter name, separated by space
The method can be divided into four classes depending on whether the method has parameters or not, and whether it has a return value:
Ø no parameter no return value method
Ø non-parametric return value method
Ø no return value method with parameters
Ø method of return value with parameter
Call Method:
1) Call alone: no concrete results
2) Output call: Good enough, write data to die, direct output, can not be a step forward to the results of the demand
3) Assignment invocation
Example: In the previous example, we added methods to optimize the sequencing process
Question: Through previous studies, let's do a more complex set of program requirements as follows
1) Enter a set of arrays 2) output of an array 3) inverse output of an array 4) can filter out the maximum and minimum number and output 5) to increase the data query function 6) using the method to optimize the procedure
Package Example;import Java.util.scanner;public class Ex1 {public static void main (string[] args) {@SuppressWarnings (" Resource ") Scanner sc = new Scanner (system.in); Int[] score = new INT[5]; for (int i = 0; i<=score.length-1; i++) {System.out.println ("Please enter" + (i+1) + "number"); int x = Sc.nextint (); Score[i] = x; } System.out.print ("The array you entered is:"); PrintArray (score); Max (score, 0); Min (score, 0); NX (score,0); PrintArray (score); where (score);} Iterate array method public static void PrintArray (int[] score) {for (int x = 0; x<=score.length-1; x + +) {if (x==0) {System.out.prin T ("[" +score[x]);} else if (x==score.length-1) {System.out.print ("" +score[x]+ "]"); }else {System.out.print ("" +score[x]); }}system.out.println (""); System.out.println ("------------------------");} Output maximum method public static void Max (int[] score, int max) {max = score[0];for (int y = 1; y<=score.length-1; y++) {if (Max<score[y]) {max = Score[y];}} System.out.println ("The maximum value you enter is:" +max); System.out.println ("------------------------");} Output minimum method public static void Min (int[] score, int min) {min = score[0];for (int y = 1; y<=score.length-1; y++) {if (min >score[y]) {min = Score[y];}} System.out.println ("The minimum value you enter is:" +min); System.out.println ("------------------------");} Reverse method public static void NX (int[] Score,int temp) {for (int y = 0; y<= (score.length-1)/2; y++) {temp = score[y];score[ Y]=score[score.length-1-y];score[score.length-1-y]=temp;} System.out.print ("The inverse of the array is:");} Find data method public static void where (int[] score) {@SuppressWarnings ("resource") Scanner sc = new Scanner (system.in); System.out.print ("Please enter the number you want to find:"); int ex = Sc.nextint (); for (int y = 0; Y<score.length; y++) {if (ex = Score[y]) {System.out.println ("You have entered the number" + (y+1) + "); else if (ex!=score[0] && ex!=score[1] && ex!=score[2] && ex!=score[3] && Ex!=score[4] {System.out.println ("No number you are looking for");
Fill: Overloading of methods
If the same class contains two or more than two methods with the same name, the number of method arguments, the order, or a different type of method, it is called an overload of the method, or it can be said that the method is overloaded. The 4 method names shown below are show, but the parameters of the methods are different and therefore belong to the overloads of the method:
How do you differentiate which overloaded method is called?
When the overloaded method is called, Java determines which overloaded method should be called based on the number and type of arguments, and the method that exactly matches the parameters is executed
At this point the result is:
PS: The basis of judging method overloading
1. Must be in the same class
2. Same method name
3, the number of method parameters, order or type is different
4, not related to the modifier or return value of a method
# # # #END # #
Related articles:
Introduction to Java Tutorial (iii) Basic syntax (1)
Introduction to Java Tutorial (iv) Basic syntax (2)