Zero-based learning java ------ day5 ------ do .... while loop

1 do ... while loop

format

Initialization statement;
 do { 
    loop statement; 
    controlling conditional statement; 
} the while (judgment condition statement); 
flow: 
    Execute the initial statement 
    then execute loop statement 
    then perform conditional control statements 
    determines do conditions: 
        to true , the loop continues and conditions control
         false loop end 
features: 
    the loop body statement is executed at least once

Case

1. do ... while seeking in the odd and even and 1-100 and

package com._51doit.javase.day04.loop;

public class DoWhile {
    public static void main(String[] args) {
        int i=1;
        int sum1 = 0;
        int sum2 = 0;
        do {
            //System.out.println("我是你爹");
            if(i%2==0) {
                sum1 += i;
            }else {
                sum2 += i;
            }
            i++;
        }the while (I <= 100 ); 
        System.out.println ( "is an even number:" + SUM1); 
        System.out.println ( "number is odd:" + SUM2); 
    } 
}

 

1.1 cycle integrated case

 Snake:

Requirements: Enter a number on the keyboard, if it is 1, represents the snake to eat the food, plus 10 points and continues to enter, if the input is a non-numeric 1, stop the program, output score

Package Penalty for com._51doit.javase.day04.loop; 

Import java.util.Scanner; 

public  class Snake {
     public  static  void main (String [] args) { 
        System.out.println ( "Welcome to the snake" ); 
        System .out.println ( "enter a number:" );
         int Score = 0 ; 
        Scanner SC = new new Scanner (the System.in);
         int NUM = sc.nextInt ();
         the while (NUM ==. 1 ) { 
            Score + = 10 ; 
            NUM = sc.nextInt ();   //For num reassigned 
        } 
        System.out.println ( "Your score is:" + Score); 
    } 
}

1.2 Login exercise cycle

Requirements: 1 console prompt the user to enter a password; 2 user to enter a password; 3 if the password entered by the user is not equal to 1234, return to step 1; 4 if the password entered by the user is equal to 1234, indicating a successful login

Analysis:
the need to use Scanner
loop: perform many times: uncertain: while
condition loop: the password entered by the user is not equal to 1234
loop: prompt the user for a password, user password

Package Penalty for com._51doit.javase.day04.loop; 

Import java.util.Scanner; 

public  class LoginDemo {
     public  static  void main (String [] args) { 
        System.out.println ( "Please enter your password" ); 
        Scanner sc = new new Scanner (System.in);
         int password = sc.nextInt ();
         the while (password = 1234! ) { 
            System.out.println ( "password is incorrect, please re-enter your password" ); 
            password = sc.nextInt ( ); 
        } 
        System.out.println ( "Login successful" );
    }
}

To do with this case in the form of do ... while

 1 package com._51doit.javase.day04.loop;
 2 
 3 import java.util.Scanner;
 4 
 5 public class LoginDemo {
 6     public static void main(String[] args) {        
 7         Scanner sc = new Scanner(System.in);
 8         do {
 9             System.out.println("请输入您的密码:");
10             int password = sc.nextInt();
11         }while(password != 1234);
12     }
13 }

Note that the above problems will be the eleventh line of code, password scoping line 10 is in the do {}, so that while line 11 is equivalent to no definition, can not be used, the solution is declared variable, the code as follows

. 1  Package com._51doit.javase.day04.loop;
 2  
. 3  Import java.util.Scanner;
 . 4  
. 5  public  class LoginDemo {
 . 6      public  static  void main (String [] args) {        
 . 7          Scanner SC = new new Scanner (the System.in) ;
 . 8          int password; // declare variables, local variables can not be used without the initial values
 . 9          do {
 10              System.out.println ( "Please enter your password:" );
 . 11              password = sc.nextInt ();
 12 is          } the while (password = 1234! );
13 is          System.out.println ( "successful login" );
 14      }
 15 }

 

1.3 Appeals code will rewrite the password string

When comparing the content of the string can not be used  "==" , "! =" In

In comparison, when the contents of the string, use the equals

Usage: a.equals String (String B); Returns true if the contents are the same, false otherwise

 

Guess you like

Origin www.cnblogs.com/jj1106/p/11302099.html