java usual cycle

A for loop  
      First judge, then execute
 
 ① for (int I = 0; I <args.length; I ++) brackets are loop loop variable type variable name; ranges loop variable; loop variable calculation {   }
   

  ② for (int I: integers) in parentheses were as follows: circulating loop variable type variable name: objects to be traversed { }    
   
 
Usage: if the number of cycles is determined generally for loop
Examples
① seeking less than 100 and
int SUM = 0 ; 
 
   for ( int I = 0; I <100; I ++ ) { 
SUM + I + =. 1 ; 
       } 
    System.out.println ( "less than 100 and is:" + sum);

Operating results as follows:

Two switch cycle

 

switch (key) required in brackets variable cycle judgment {
  Case value:    BREAK;
   

  default:
   break;
  }

Usage: used for ships branched, or selected.
 
Examples
② do a vending machine
System.out.println ( "Please select trade name:" ); 
  System.out.println ( "1. 2. tea green tea 3. 4. 5. wahaha pulsation" ); 
  Scanner SCA = new new Scanner (the System.in) ;
   int User = sca.nextInt ();
   Switch (User) {
   Case . 1 : 
   System.out.println ( "green tea 1. 3 yuan" );
    BREAK ;
   Case 2 : 
   System.out.println ( "black 2. 3 yuan " );
    BREAK ;
   Case . 3 : 
   System.out.println ( " ripple 3. 4 yuan " );
    BREAK ;
   Case . 4: 
   System.out.println ( "tea 4. 5 yuan" );
    BREAK ;
   Case . 5 :
   Case . 6 : 
   System.out.println ( "5. The wahaha 5 yuan" );
    BREAK ;
   default : 
   System.out.println ( " no such product " );
    BREAK ; 
  }

5 or 6 is selected the same result

The results are:

 Three while loop
 
First judge, then execute
 
while (condition) {loop condition in parentheses   }
   

  
 Usage: When the conditional expression is true, the loop body is executed, and then judge, and so, if the conditions are not met, it is possible to perform not once. General circulation cycles for uncertain
 
Examples
 ③ Calculation of less than 100 can be the sum of the number divisible by 3

 

int i=0;
  int sum=0;
  while (i<100){
  if (i%3==0) {
   sum+=i;
           }
   i++;
 }
  System.out.println("100以内的能被3整除之数的和为"+sum);

运行结果为

 四   dowhile 循环
 
 先执行,再判断
 
do {
   
  } while (condition);括号内为循环条件
 
 使用方法:一般用于循环次数不确定的循环,与 while 循环不同的是  dowhile 循环先执行后判断,至少执行一次.
 
 实例为
④  计算100以内即能被5整除又能被7整除数的和

 

int j=1;
  int sum2=0;
  do {
   if (j%5==0&&(j%7==0)) 
    sum2 +=j;
    j++;
  } while (j<=100);
  
  System.out.println("100以内即能被5整除又能被7整除数的和"+sum2);

运行结果为

 

Guess you like

Origin www.cnblogs.com/qq1312583369/p/11161757.html