java循环语句和条件句

while 循环

int i = 0while(i < 10){

    i++; //变量的值增加1

    System.out.println(i); //输出变量的值

    }

for循环

/* 建立一个Collection */
 String[] strings = {"A""B""C""D"};
 Collection list = java.util.Arrays.asList(strings);

 /* 开始遍历 */
 for (Object str : list) {
     System.out.println(str); /* 依次输出“A”、“B”、“C”、“D” */
 }
或是  

/* 建立一个数组 */
 int[] integers = {1234};
 /* 开始遍历 */
 for (int j = 0; j < integers.length; j++) {
     int i = integers[j];
     System.out.println(i);
 } 

或是

/* 建立一个Collection */
 String[] strings = {"A""B""C""D"};
 Collection stringList = java.util.Arrays.asList(strings);
 /* 开始遍历 */
 for (Iterator itr = stringList.iterator(); itr.hasNext();) {
     Object str = itr.next();
     System.out.println(str);
 }

while

public static void main(String[] args){
int a=1,result=0;
do{
result+=a++;
}while(a<=100);
System.out.println(result);
}

if else

   int a = 10ifa >= 0)

    System.out.println(“a是正数”);

    ifa % 2 == 0)

    System.out.println(“a是偶数”);

switch

public static void test(int index) {  
    switch (index) {  
    case 1:  
        System.out.println(1);  
    case 2:  
        System.out.println(2);  
    case 3:  
        System.out.println(3);  
    default:  
        System.out.println("Default");  
    }  
}  

猜你喜欢

转载自blog.csdn.net/a520songhai/article/details/81000771