循环结构
While 循环
package operator;
public class WhileDemo01 {
public static void main ( String[ ] args) {
int i = 0 ;
while ( i< 100 ) {
i++ ;
System. out. println ( i) ;
}
}
}
package operator;
public class WhileDemo02 {
public static void main ( String[ ] args) {
while ( true ) {
}
}
}
package operator;
public class WhileDemo03 {
public static void main ( String[ ] args) {
int i= 0 ;
int sum = 0 ;
while ( i<= 100 ) {
sum = sum + i;
i++ ;
}
System. out. println ( sum) ;
}
}
Do While
for循环
package operator;
public class ForDemo01 {
public static void main ( String[ ] args) {
int a = 1 ;
while ( a<= 100 ) {
System. out. println ( a) ;
a+= 2 ;
}
System. out. println ( "============================" ) ;
for ( int i= 1 ; i<= 100 ; i++ ) {
System. out. println ( i) ;
}
for ( ; ; ) {
System. out. println ( "死循环开始" ) ;
}
}
}
package operator;
import java. net. SocketTimeoutException;
import java. util. ArrayList;
import java. util. List;
public class ForDemo02 {
public static void main ( String[ ] args) {
int oddSum= 0 ;
int evenSum= 0 ;
for ( int i = 0 ; i < 100 ; i++ ) {
if ( i% 2 != 0 ) {
oddSum += i;
} else {
evenSum += i;
}
}
System. out. println ( "奇数的和" + oddSum) ;
System. out. println ( "偶数的和" + evenSum) ;
}
}
package operator;
public class ForDemo03 {
public static void main ( String[ ] args) {
for ( int i = 0 ; i <= 1000 ; i++ ) {
if ( i% 5 == 0 ) {
System. out. print ( i+ "\t" ) ;
}
if ( i% ( 5 * 3 ) == 0 ) {
System. out. println ( ) ;
}
}
}
}
打印九九乘法表
package operator;
public class ForDemo04 {
public static void main ( String[ ] args) {
for ( int i= 1 ; i<= 9 ; i++ ) {
for ( int a= 1 ; a<= i; a++ ) {
System. out. print ( i + "X" + a + "=" + ( a * i) + "\t" ) ;
}
System. out. println ( ) ;
}
}
}
增强for循环
public class ForDemo05 {
public static void main ( String[ ] args) {
int [ ] numbers = { 10 , 20 , 30 , 40 , 50 } ;
for ( int i = 0 ; i< 5 ; i++ ) {
System. out. println ( numbers[ i] ) ;
}
System. out. println ( "=================" ) ;
for ( int x: numbers) {
System. out. println ( x) ;
}
}
}