Java 循环结构语句使用(上)

public class WhileClass {

    public static void main(String[] args) {
            //输出10-1
            int a = 10;
            while(a >= 1){

                System.out.println("数字为"+a);
                a--;
            }
            //输出1-100的和
            int b = 1;
            int c = 0;
            while(b <= 100){
                c = c + b;
                b++;
            }
            System.out.println("1-100的和为"+c);
            //输出1-100的奇数和与偶数和
            int j = 1;
            int jh = 0;
            int oh = 0;
            while(j <= 100){ 
                if(j % 2 != 0 ){
                    jh = jh + j;    
                }else if(j % 2 == 0){
                    oh = oh + j;
                }
                j++;
            }
            System.out.println("1-100的奇数和为"+jh);
            System.out.println("1-100的偶数和为"+oh);
        }
    }
    /*
     * 循环四要素:
     * 1、循环初始变量
     * 2、循环控制条件
     * 3、循环体
     * 4、循环迭代变量
     * 
     */
    /*
     * while结构:
     * while(布尔表达式){
     *      执行语句...当表达式结果为true时才执行
     * }
     */

猜你喜欢

转载自blog.csdn.net/ilovehua521/article/details/81914603