Java basics 4: java Basics
1 for loop nesting
- In short, there is a for loop statement in a for loop statement.
- The outer layer loops, once every cycle, and once every week.
Package java003. * Note: */public class ForForDemo {public static void main (String [] args) {for (int x = 0; x <= 3; x ++) {System. out. print ("x =" + x + ""); for (int y = 0; y <= 4; y ++) {System. out. print ("y =" + y);} System. out. println ();}}}
- Exercise: print a rectangle with four rows and five columns
Package java003. * Note: */public class ForForTest {public static void main (String [] args) {for (int x = 1; x <= 4; x ++) {for (int y = 1; y <= 5; y ++) {System. out. print ("*");} System. out. println ();}}}
Package java003. * Note: */public class ForForTest {public static void main (String [] args) {for (int x = 1; x <= 5; x ++) {for (int y = 0; y <= 5-x; y ++) {System. out. print ("*");} System. out. println ();}}}
Package java003. * Note: */public class ForForTest {public static void main (String [] args) {for (int x = 1; x <= 5; x ++) {for (int y = x; y <= 5; y ++) {System. out. print ("*");} System. out. println ();}}}
Package java003. * Note: */public class ForForTest {public static void main (String [] args) {for (int x = 1; x <= 5; x ++) {for (int y = 1; y <= x; y ++) {System. out. print ("*");} System. out. println ();}}}
- Example: multiplication table
Package java003. * Note: */public class ForForTest {public static void main (String [] args) {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 ();}}}
2. Other process control statements (break and continue)
- Break)
- Application Scope:
- Switch statement
- Loop statement
- Note:
- When a break statement exists independently, other statements cannot be defined under the break statement because it cannot be executed.
- When the loop is a multi-tier loop, break jumps out of the inner loop by default.
- Continue (continue)
Package java004;/*** release /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 define a function
- What is a function?
- A function is an independent applet defined in a class with specific functions.
- A function is also called a method.
- Function Format:
Access modifier Return Value Type Function Name (parameter type form parameter 1, parameter type form parameter 2 ,......) {Execution statement; return value;} return value type: the data type of the result after the function is run. Parameter type: it is the data type of a formal parameter. Form parameter: a variable used to store the actual parameters passed to the function when a function is called. Actual parameter: the specific value passed to the form parameter. Return: Used to end a function. Return Value: The result of the function operation. The result is returned to the caller.
Package java004;/**** Note: */public class FunctionDemo {public static void main (String [] args) {int result = FunctionDemo. add (1, 2); System. out. println ("result =" + result);} public static int add (int a, int B) {return a + B ;}}
3.2 features of functions
- If the function does not return a specific value
- ① Return is followed by a semicolon.
- ② The type of the return value is void. in Java, void indicates that the function does not return a specific value.
- ③ If the return value type is void, the return Statement in the function can be omitted without writing (specific analysis of specific issues ).
- Define a function to encapsulate the function code.
- This feature is easy to reuse.
- A function is executed only when it is called.
- The emergence of functions improves code reuse.
- Note:
- You cannot call a function or define a function in a function.
- When defining a function, the function result should be returned to the caller and handled by the caller.
3.3 function overload)
- Overload:
- Concept: in the same class, more than one function with the same name is allowed, as long as they have different numbers or types of parameters.
- Feature: it has nothing to do with the return value. It only looks at the parameter list and function name.
- Benefits of heavy load: It facilitates reading and optimizes programming.
Package java004;/**** Note: heavy load */public class OverLoad {public static void main (String [] args) {int result = add (); System. out. println ("two integers and:" + result); result = add (1, 2, 3); System. out. println ("Three integers and:" + result); double d = add (3.0, 4.0); System. out. println ("two floating point numbers and:" + d );} /*** calculate the sum of two integers * @ param a * @ param B * @ return */public static int add (int a, int B) {return a + B ;} /*** calculate the sum of two floating point numbers * @ param a * @ param B * @ return */public static double add (double a, double B) {return a + B ;} /*** calculate the sum of the three integers * @ param a * @ param B * @ param c * @ return */public static int add (int a, int B, int c) {return a + B + c ;}}