day05 generates a random number, do-while loops, for loops, loop flow control (and Continue BREAK), nested loops

review

1. multiple if

  In the case of a condition triggered when three or more

  if (<Condition 1>) {

      1 // condition is true run

  } Else if (<Condition 2>) {

      // Condition 1 is false, to run true condition 2

  } Else if (<Condition 3>) {

      // conditions 1 and 2 are false, Condition 3 is running true

  }else{

      // run-time conditions are false, 2, 3

  }

 

2.switch structure

  switch (<integer expression>) {

  case 1:

     // integer result of the expression to run 1

     break;

  case 2:

     // integer result of the expression to run 2

     break;

  ....

  default:

     Run-time did not match the results of an integer expression //

  }

3. The advantages and disadvantages of various structures

1) if a range determined by multiple

2) contour is determined by switch

 

4. cyclic structure

1) What is the cycle

  Repeatedly run the same or similar code

2) Why use cycle

  Reduce code redundancy , improve the maintainability of the program

3) elements of the cycle

  3.1) loop variable

  3.2) cycling conditions

  3.3) Operation cycle

  3.4) variable update

 

5.while cycle

  while (<condition>) {

     // condition is true when you run code

  }

 

notes

1 generates a random number

  1.1) java.util.Random import;

  1.2)Random ran=new Random();

  1.3) generates integer

      int  XXX = ran.nextInt (<random range>);

      xxx value is possible

      0 ~ random range -1

      E.g. ran.nextInt (10)

      Possible values is 0 to 9,

 

2.do-while loop

  grammar

 

  do{

     // condition is true when you run code

  } While (<condition>);

 

3.while and do-while differences loop

1) while loop:

  First determine , after execution

  Initial conditions are not met , do not perform a

2) do-while loop:

  First execution after judgment

  Initial conditions are not met , will be performed at least once

3) When understood, understood as:

  while loop than do-while loop

  More than one judge

 

4.for cycle

1) for loop syntax

  for (Expression 1; 2 Expression; Expression 3) {

      // loop

  }

  1.1)

  Expression 1: Declare variables

  Expression 2: cycling conditions

  Expressions update variables: 3

 

  1.2)

  Expression , 2, 3, can be omitted

  Expression 2 is omitted defaults to true

  However, two ; can be omitted

  

  1.3)

  Expression 1 can declare multiple variables,

                       With , separated

 

  Expression 3 can update multiple variables

                       With , separated

  1.4) for the processing loop for

      Cycles cycles fixed

 

The cyclic structure Summary

1) determining the number of cycles used for

2) the number of cycles of use uncertain

              while或do-while

  2.1) when the time ......

      Use while

 

  2.2) until the time .....

      Use do-while

 

6. The circulation flow control

1) break keyword

  The terminating switch can break switch structure

  The cyclic structure break can terminate the loop structure

  while, do-while, for all you can use

 

2) continue keyword

  It can only be used in a loop structure

  Cycle operation to continue the effect that:

  Over code is not running the current cycle

  Continue to run the next cycle

 

7. nested loop

1) What is the nested loop

  A loop structure

  Also contains another complete cycle structure

2) loop nests Notes

  2.1) one cycle of the outer loop

      Memory cycle cycle again

  2.2) flow control keywords (break, continue)

      In the inner loop

      It affects only the inner loop

  2.3)while\dowhile\for

      Can be arbitrarily nested

  2.4) Any cyclic nesting level

      You should avoid excessive levels of nesting

 

 

 

operation

1) in accordance with the off-season season ticket prices,

First class and economy class fees,

Enter the original ticket, month and 1 first class or economy class 2,

In which the high season ( May to October)

First 10%, 15% economy class,

Off-season ( November through April)

First 7 fold, 65 fold economy class,

The final output of ticket prices

        Scan = Scanner new new Scanner (System.in); 
        System.out.println ( "Please enter the Ticket Price" );
         Double . Price = scan.nextDouble (); 
        System.out.println ( "Please enter the month" );
         int month The = scan.nextInt (); 
        System.out.println ( "class' (2. 1. First Economy)" );
         int level = scan.nextInt ();
         // season 
        IF (month The> =. 5 && month The <= 10) { // First 
            IF (Level ==. 1 ) { 
                . price * = 0.9 ; 
            } the else {// economy 
                . Price * = 0.85 ; 
            } 
        } the else { // season 
            IF (Level ==. 1) { // First 
                . Price * = 0.7 ; 
            } the else { 
                . Price * = 0.65 ; 
            } 
        } 
        System.out.println ( " ticket price after the discount is: "+ price);
View Code

 

do-while

1) using a do-while loop to write

  Xiao Ming first month's salary 4000

  Last month, a month later than 200

  Calculate the 12-month total Xiaoming how much they will get wages

 

        int sum=0;
        int i=1;
        int sal=4000;
        do {
            sum+=sal;
            sal+=200;
            i++;
        }while(i<=12);
        System.out.println(sum);
View Code

 

2) prepared using the do-while loop

  Xiao Ming first month's salary 4000

  Last month, a month later than 200

  When Bob got a total of more than wages

  150000 number of months needed

 

        int i=1;
        int sum=0;
        int sal=4000;
        do {
            sum+=sal;
            System.out.println("sum:"+sum+",i:"+i+",sal:"+sal);
            sal+=200;
            i++;
        }while(sum<150000);
        
        System.out.println(i-1);
View Code

 

Exercises for loop

1) User input 5 student achievement in the loop

  Calculate their average

 

        Scan = Scanner new new Scanner (the System.in);
         int SUM = 0 ;
         for ( int I =. 1; I <=. 5; I ++ ) { 
            System.out.println ( "Please enter" + i + "grade students' ) ;
             int Score = scan.nextInt (); 
            SUM + = Score; 
        } 
        Double AVG = SUM * 1.0 /. 5 ; 
        System.out.println ( "average:" + avg);
View Code

 

break

1) write a loop tips:

The user input cycle      while (true) {

If the user inputs a digital      ... while (true) {

The number is negative          }

  End of the cycle          

 

        Scan = Scanner new new Scanner (the System.in);
         // user input negative loop exits 
        the while ( to true ) { 
            System.out.println ( "Enter a number" );
             int NUM = scan.nextInt ();
             IF (NUM <0 ) {
                 // NUM is less than zero, exit 
                BREAK ; 
            } 
            
        }
View Code

 

2) write a loop

  Loop user inputs the password ( int type )

  If the user input is 123456 (right)

  End of the cycle

  And output cycle several times

 

        Scan = Scanner new new Scanner (the System.in);
         int I =. 1 ;
         do { 
            System.out.println ( "Please enter the password" );
             int pwd = scan.nextInt ();
             // correct password loop exits 
            IF (pwd = 123456 = ) {
                 BREAK ; 
            } 
            System.out.println ( "wrong password, to re-enter" ); 
            I ++ ; 
        } the while ( to true ); 
        System.out.println ( "total input" + i + "views");
View Code

 

Nested loop

1) use of nested loops to achieve the output

  Multiplication table

 

        for(int i=1;i<=9;i++) {

            for(int j=1;j<=i;j++) {
                System.out.print(
                        j+"×"+i+"="+i*j+" ");
            }
            System.out.println();
        }
View Code

 

Guess you like

Origin www.cnblogs.com/hxun/p/11541202.html