Java/for Loop/recursive function loop

Source: Internet
Author: User

First: Java's focus and difficulty, naming and caching

This time our content mainly uses:

    naming rules: uppercase and lowercase letters, underscores, dollar sign $, numbers, and numbers do not begin

    Declaration of variables: data types divide memory space, name, assign value

    declaration of the method: modifier list, return value, method name, parameter list, method body

 Example:

1, if you want to calculate 1~100 's odd and, even, and, there are 1~100 and;

2, there are 1~n and

3, 99 multiplication table

The first question, 1~100 's odd and, even, and, and

Write A For loop first

  For loop, the run rule for the For loop, for (expression 1, expression 2, expression 3), executes the expression 1, and executes only once, the expression 2 is judged, if correct, then runs the following program, after the completion of the run, loop to the expression 3, and then judge the expression 2, Until the expression 2 evaluates the error, it terminates the run of the loop.
Loop three elements: initial value (expression 1), termination condition (expression 2), step (expression 3), Expression 2 must be Boolean type

public class for_ji_ou{//Declares a class with public, and can have more than one public in each Java file, but only one class that is decorated with public, the class name of which must be the same as the file name
public static void Main (string[] args) {//main method, which can be said that all programs are executed in the Main method
int ji = 0; With the int data type, open an int length space, and give the space a name Ji, and assign it a value of 0;
int ou = 0; With the int data type, open an int length space, and give the space a name OU, and assign it a value of 0;
int sum = 0; With the int data type, open an int length space, and give the space a name of sum, and assign it a value of 0;
for (int i=0; I <=100; i+=2) {//for loop, initial value 0, terminating condition = 100, Step 2
ou + = i; OU is we have declared a good variable, let ou + = I first + = is the bar around two values added, and then the added value to the left, you already know what meaning, it is every time the loop out of I and OU add,
}
System.out.println ("The even number of 1-100" is: "+ou); Output
for (int i=1; I <=100; i+=2) {//Initial value 1
Ji + = i;
}
System.out.println ("1-100 Odd and for:" +ji);
for (int i=0; I <=100; ++i) {//initial value is 1, step is 1
sum + = i;
}
System.out.println ("1-100 's and for:" +sum);
}
}
write with If

if () {if judged correctly, execute the following statement

Java statements

}else{ Otherwise, execute the following statement

Java statements

}

public class if{
public static void Main (string[] args) {
int j = 0; Same as the For loop above.
int o = 0;
for (int i=0; I <=100; ++i) {//initial value 0, step 1


if (i% 2 = = 1) {//Here we are the remainder, if the addition of 2 to 1, then it must be odd, right? I let the odd number execute the following statement
J+=i; Add value to J each time and I
}else{//remainder is not equal to 1 or even, execute the following statement
O+=i; Add the value to o each time and I
}
}
System.out.println ("1-100 Odd and" +j); Output
System.out.println ("Even of 1-100" and "+o"); Output
}
}

The second question uses recursion to write a 1~n and

First, what is a recursive function, just like for, is a loop, a function loop,

What is recursive invocation, which is the function itself calls itself

But the function does not call do not execute, the call executes, and the bar value is returned to the call

Recursion and looping are the same, there are three elements in common: initial value, termination condition, step size

public class di_gui{//portals, public decorated classes
public static void Main (string[] args) {//main method
int s = 100; The int data type declares the variable s and assigns a value of 100
int q = A.M1 (s); The int data type declares that the value assigned by the Q and assignment is a function call, first the function is finished, in order to assign a value, after the assignment is completed, you can proceed to the next instruction because Java runs from top to bottom, from left to right.
SYSTEM.OUT.PRINTLN (q); Output P
}
}
Class A {//Create a class file as a
public static int M1 (int n) {//Creates a M1 method and is decorated with static, so this method is a static method, called by a static method: Class name. Method Name (); If you are in a scope: Method name ();
if (n==1) {//If n==1 executes the following statement
return 1; Return returns a value of 1 if you run to this step, assign the value to M1, terminate the entire function
}else{//n is not equal to 1, execute the following statement
Return n+m1 (n-1); This is the focus of the return value is a function, I want to return the value, I have to run the function out of it, and this function, I pass the argument is n-1, and not n so no matter how much N, sooner or later, N to 1
}

}
}

Return n+m1 (n-1); Operation approximate order, see below
/*

10+M1 (9) +9+m1 (8) +.....+m1 (1)

If the above S equals 10, it is equal to M1 (10);
M1 (10); When M1 (10), the value is = 10+M1 (9) and finally look at the back = 10+9+8+7+6+5+4+3+2+1; M1 (9); when M1 (9), the value is = 9 +m1 (8) and finally look at the back = 9+8+7+6+5+4+3+2+1;
M1 (8) when M1 (8), the value is = 8 +m1 (7) Finally look at the back of the =8+7+6+5+4+3+2+1;
M1 (7) when M1 (7), the value is = 7 +m1 (6) Finally look at the back of the =7+6+5+4+3+2+1;
M1 (6) when M1 (6), the value is = 6 +m1 (5) Finally look at the back of the =6+5+4+3+2+1;
M1 (5) when M1 (5), the value is = 5 +M1 (4) Finally look at the back of the =5+4+3+2+1;
M1 (4) when M1 (4), the value is = 4 +M1 (3) Finally look at the back of the =4+3+2+1;
M1 (3) when M1 (3), the value is = 3 +M1 (2) Finally look at the back of the =3+2+1;
M1 (2) when M1 (2), the value is = 2 +m1 (1) Finally look at the back of the =2+1;
M1 (1) when M1 (1), the value is = 1 (because when N=1 is, the return value returned is 1 so the function is terminated, and 1 is returned to M1)
*/

Title Three: 99 multiplication table

Loop three elements: initial value, termination condition, step size

/*
To print a 99 multiplication table using a nested for loop

1*1=1
2*1=2 2*2=4
.......
9*1=9...............9*9=81

The number of rows is equal to the maximum number of squares

*/

public class for_cheng_fa_biao{//public decorated class, and there's only one, needless to say.
public static void Main (string[] args) {//main method
for (int i=1; I <=9; ++i) {//Loop, remember, the loop does not loop again, will put the value of the inside all run out, just calculate the loop again, so I this nested loop, every time the outside of the loop cycle, inside that cycle will be finished;
for (int j=1; J <= i; ++j) {//Termination condition, cannot be greater than I
System.out.print (j + "*" + i + "=" + (I*J) + ""); Output, the result is that the contents of the j*i= (J*i) string are directly output, and the string and the value of the connector are +
}
System.out.println (); Output a newline, without words, will be in a row past, affecting the beautiful
}
}
}

OK, finished, we have a good understanding of the recursive function of the return value where, more understanding, where to write bad, wrong, please a lot of advice

                                                      

                                                        The Gentleman ゝ Tutu.

Java/for Loop/recursive function loop

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.