1 For loop nesting
- In short, there is a for loop statement inside, and a For loop statement.
- Outer Loop, one cycle at a time, inner loop, cycle for one week.
Packagejava003;/*** 2017/9/1. * Description:*/ Public classForfordemo { Public Static voidMain (string[] args) { for(int× = 0;x<= 3; x + +) {System.out.print ("X=" +x+ ""); for(inty = 0;y<=4;y++) {System.out.print ("Y=" +y); } System.out.println (); } }}
- Exercise: Print a four-row, 5-column Rectangle
package java003; /** * 2017/9/1. * Description: */ public class forfortest { public static void main (string[] args) { /span>for (int x = 1; x <= 4; X++ for (int y = 1; y<= 5;y++ "*"
package java003; /** * 2017/9/1. * Description: */ public class forfortest { public static void main (string[] args) { /span>for (int x = 1; x <= 5; X++ for (int y = 0; y<= 5-x;y++ "*"
package java003; /** * 2017/9/1. * Description: */ public class forfortest { public static void main (string[] args) { /span>for (int x = 1; x <= 5; X++ for (int y = X y<= 5; Y++ "*"
package java003; /** * 2017/9/1. * Description: */ public class forfortest { public static void main (string[] args) { /span>for (int x = 1; x <= 5; X++ for (int y = 1; y<= x; Y++ "*"
- Example: 99 multiplication table
Packagejava003;/*** 2017/9/1. * Description:*/ Public classForfortest { Public Static voidMain (string[] args) { for(intx = 1;x<=9;x++){ for(inty = 1; Y <= x; y++) {System.out.print (y+ "*" +x + "=" + (y*x) + "\ T"); } System.out.println (); } }}
2 Other flow control statements (break and Continue)
- Break (jump out)
- Application Range:
- Switch statement
- Looping statements
- Note :
- When the break statement exists alone, no other statements can be defined under the break statement because it cannot be executed.
- When the loop is a multi-layer loop, break defaults to the inner loop.
- Continue (continued)
package java004; /** * 2017/9/2 * Description: */ public class Breakcontinuedemo { public static void main (string[] args) { for (int x = 0; x <=5;x++ if (x = = 2" { break ; } System.out.println ( "x=" +x); } }}
X=0
X=1
3 function (method) 3.1 Definition of function
- What is a function?
- A function is a separate applet defined in a class that has a specific function.
- Functions are also called methods.
- The format of the function:
The access modifier returns a value type function name (parameter type form parameter 1, parameter type parameter 2,......) { EXECUTE statement; return return value;} Return value type: The data type of the result after the function is run. Parameter type: is the data type of the formal parameter. Formal parameter: is a variable that is used to store the actual arguments passed to the function when the function is called. Actual parameter: The specific value passed to the formal parameter. return: Used to end the function. Return value: The result of the function's operation, which is returned to the caller.
Packagejava004;/*** 2017/9/3 * Description:*/ Public classFunctiondemo { Public Static voidMain (string[] args) {intresult = Functiondemo.add (); System.out.println ("Result=" +result); } Public Static intAddintAintb) { returnA +b; }}
3.2 Features of the function
- If the function does not have a specific return value
- ①return directly after the semicolon;
- The ② return value of type Void,java in void indicates that the function does not have a specific return value.
- ③ if the type of the return value is void, then the return statement in the function can be omitted without writing (specific problem specific analysis).
- The function code can be encapsulated by defining functions.
- Facilitates the reuse of this feature.
- Functions are executed only if they are called.
- The appearance of functions improves the reusability of code.
- Note :
- Functions cannot be called in functions, and functions cannot be defined inside a function.
- When defining a function, the result of the function should be returned to the caller for processing by the caller.
3.3 Overloading of functions (overload)
- Overload:
- Concept: In the same class, more than one function with the same name is allowed, as long as the number of arguments or the type of the parameter are different.
- Features: Regardless of the return value, see only the parameter list and function name.
- Advantages of overloading: Easy to read, optimized for programming.
Packagejava004;/*** 2017/9/3 * Description: Reload*/ Public classOverload { Public Static voidMain (string[] args) {intresult = Add (); System.out.println ("Two integers and:" +result); Result= Add (All); System.out.println ("Three integers and:" +result); DoubleD = Add (3.0,4.0); System.out.println ("Two floating-point numbers and:" +d); } /*** Ask for two integers and *@paramA *@paramb *@return */ Public Static intAddintAintb) { returnA +b; } /*** Ask for two floating-point numbers and *@paramA *@paramb *@return */ Public Static DoubleAddDoubleADoubleb) { returnA +b; } /*** Ask for three integers and *@paramA *@paramb *@paramc *@return */ Public Static intAddintAintBintc) { returnA + B +C; }}
Java Basic Four