04 loop control statements

The for loop    
• for loop is a common structural support iteration, is the most effective, most flexible loop structure 
• grammatical form for (initial expression; Boolean expression; stepper) { 
- loop body; 
•} 
• Note matters 
-for loop after executing the test conditions, the first part of the implementation of the program, and then executes a step. 
- part of the initialization variable declaration statement for which scope is for the entire body of the loop 
- "Initialization" and "conditional expression loop" portions may be used to perform a plurality of operations commas 
- if statement three parts are empty (points no not the province), the equivalent of an infinite loop 

jump statement --- break and the Continue     
• loop in any part of the body, can control the flow of the loop with the break. break quits the loop that the remaining statements are not executed. (BREAK statement can also be used in multi-branching switch statement) 
Continue statement is used in the loop body, for terminating a process cycles, i.e. skips the statements in the loop body has not been performed, it is determined whether the next cycle is followed by . 

Jump statement --- return 
return statement exits from the current method, which returns to the statement that invokes the method, and the program continues at the statement from the lower bar of the statement. 

Two formats return statement (explained in detail to the specific method)
 . 1, return expression The
A statement of the method returns a value to the caller. 
The return value data type must be declared in the same type of methods or data type declared accuracy is lower than the return value. 
2, return 
by a method as declared void return type declaration is empty, return of this type should be used, it does not return any value. BREAK -switch statement - Loops continue   - loop return - any statement, end the current method, and in fact there is no relationship cycle 
• Continue the cycle can only be used in   the while  do - the while implementation of the code back to continue after execution place to judge, to decide whether to re-determine the next execution 
• execution continue in the for loop jump directly to the i ++ iteration 

multiple loops (loop nest) 
a loop body also contains another complete cycle structure 
of any two loops are nested in each other 
can be any level cycle, but generally not more than 3 layers 
multi-cycle execution of 
the outer loop variable change time, the inner loop variable is to be changed again 
Package duochongxunhuan; 

Import java.awt.Choice;
Import java.util.Scanner; 

/ ** 
 * There are five clothing stores, each up to buy three. Users can choose to leave, you can buy clothes. Finally, a total print bought a few pieces of clothing 

ideas 
outer loop control to each store 
inner loop control process to buy clothes 
use break to exit the inner loop 

 * @author Administrator 
 * 
 * / 

public  class BreakDemo {
     public  static  void main (String [] args) { 
        Scanner INPUT = new new Scanner (the System.in);
         int COUNT = 0 ;
         for ( int I =. 1; I <. 5; I ++ ) { 
            System.out.println ( "Welcome to the first" + i + "monopoly shop " );
             for(Clothes " );int J = 0; J <. 3; J ++ ) { 
                of System.out.print ( "leaving it (Y / n-?)?" ); 
                String Choice   = input.nextLine ();
                 IF ( "Y" .equals (Choice )) {
                     BREAK ; 
                } 
                System.out.println ( "buy a piece of clothing" ); 
                COUNT ++ ; 
            } 
            System.out.println ( "Check checkout" ); 
            System.out.println ( "" ); 
        } 
        System.out.println ( "bought a total of" + count + "dress"
    }
 
} 


Package duochongxunhuan; 

Import java.util.Scanner; 

/ ** 
 * 4. 3 classes each participating students, calculated for each participating student class average, score statistic is greater than the number of 85 participants 

thinking 
the outer loop class 
inner loop a control class each student 
score is not larger than 85, Continue 
score greater than 85, plus the number of participants. 1 

 * @author Administrator 
 * 
 * / 

public  class continueDemo {
     public  static  void main (String [] args) { 
        Scanner INPUT = new new Scanner ( the System.in);
         int A = 0 ;
         int Score [] = new new  int [. 4];
         For ( int I = 0; I <. 3; I ++ ) {
             int SUM = 0 ;
             Double AVG = 0 ; 
            System.out.println ( "Please enter" (i + 1) + "+ classes results: " );
             for ( int J = 0; J <. 4; J ++ ) { 
                System.out.println ( " first "+ (j + 1) + " th student score: " ); 
                score [J] = input.nextInt (); 
                SUM = SUM + Score [J]; 
                AVG = SUM / 4.0 ;
                 IF (Score [J]> 85 ) { 
                    A ++ ;
                } 
            } 
            System.out.println ( "first" + (i + 1) + " average participating student classes are:" + AVG + "\ n-" ); 
        } 
        System.out.println ( "score 85 points or more the number of participants with a "+ a +" man " ); 
    } 

} 


Package the Test; 

/ ** 
 * *. 1. 1. 1 = 
 *. 1 * 2 * 2 = 2. 4 = 2 
 * 2. 3. 1 * * =. 3. 6. 3. 3 = =. 9. 3 * 
 * *. 1. 4 = 2. 4. 4 * * =. 8. 4. 3. 4 = 12 is = 16. 4 * 
 * ................. 
 * @author Administrator 
 * 
 * / 

public  class Test99 {
     public  static  void main (String [] args) {
        for ( int I =. 1; I <10;i++) {
             For ( int J =. 1; J <= I; J ++ ) { 
                of System.out.print (J + "*" + I + "=" + I + J * "\ T" ); 
            } 
            the System.out. the println (); 
        } 
    } 

} 




• What is the method 
- the statement together to perform the operations of packaging, a function used to perform operations 
- called procedure or function in some languages 
- special method main, a program executed by an inlet 
public  static  void main (String [] args) { 
implement functions statement 
} 
- unlikely that all functions are placed in the main, to define other methods to complete the specified functions, when required to call a method 
Package the Test;
 / ** 
 * 2 . Analyzing the number of prime numbers 1-100 and outputs all primes. 
 * (Will judge whether a number is a prime feature extraction method into,
 * In the cycle called directly to) 
 * / 
public  class sushuDemo {
     public  static  void main (String [] args) {
         int COUNT = 0 ;
         for ( int I =. 1; I <= 100; I ++ ) {
             IF (the isPrime ( I)) { 
                System.out.println (I); 
                COUNT ++ ; 
            } 
        } 
        System.out.println ( "total number of prime numbers is 1 to 100:" + COUNT); 
        } 
    public  static  Boolean the isPrime ( int NUM) {
         boolean= In Flag to true ;
         int count = 0 
modifier: when encapsulation repeat, the method determines the operating range;
         Int I = 2 ;
         IF (NUM ==. 1 ) { 
            In Flag = to false ; 
        } the else {
             for (I = 2; I <= the Math.sqrt (NUM); I ++ ) {
                 IF (I NUM% == 0 ) { 
                    In Flag = to false ;
                     return In Flag; 
                } 
            } 
        } 
        return In Flag;     
    } 
} 

return value type: required, if no return value shall write void. The method returns a value only 
method name: 
parameter list: can be 0, 1, a plurality, need to be described type. Called formal parameters 
Procedure: accomplish specific functions. If there is a return value, you must have a return statement; if no return value, the default is the last statement return, it can be omitted. 

Method overloading: - can be defined in a class with the same name, but the parameters of a plurality of different methods 
- when invoked, will select the corresponding parameter table according to different methods. 
• determining based on 
- the same class 
- the same method name 
- different: a list of different parameters (type, number, in a different order) 
• Note 
- Only the overload does not establish a return value of the method ( 
  int    A (String STR) {},
   void   a String {I} 
- parameter names different only, does not constitute a method of overload 
• [ . 1 ] method defined function code can be encapsulated 
• [ 2 ] the method is performed will be called only 
• [ . 3 ] method improved reusability of code appears 
• [ 4 ] method can only be invoked method, the method can not be defined within the method
 * [5 Time] defined method, the method should return the results to the caller to handle 

• What is recursion (recursion ) 
- a program that calls itself is called recursive programming techniques. 
- a procedure or function calls itself directly or indirectly a method in its definition or description 
recursion features 
a simple problem can be decomposed into several sub-problems layer
Sub-problems and their solutions upper consistent problem 
solving the problem depends on the outer sub-problem solving 
Package the Test;
 Import java.rmi.dgc.DGC;
 / ** 
 * 4. write a recursive algorithm program: a number of rules are as follows: 
 * 1,1,2,3,5,8,13,21,34 ...... 
 * 40-digit number is the number of columns required. 
 * @Author Administrator 
 * 
 * / 
public  class digui {
     public  static  void main (String [] args) { 
        System.out.println ( "The result is:" + Dg (2 )); 
    } 
    public  static  int Dg ( int I) {
         IF (I <= 0 ) {
             return 0 ;
        }else if(i>0 && i<= 2){
            return 1;
        }
        return Dg(i-1)+Dg(i-2);
    }
}

 

Guess you like

Origin www.cnblogs.com/meizhoulqp/p/11241302.html