java的基础应用

a++和++a的区别

package com.lol.LiOu.operator;

public class Demo04 {
    public static void main(String[] args) {
        //++  --  自增 自减  一元运算符
        int a = 3;

        int b = a++; //a++  a = a + 1 //执行完这行代码后,先给b赋值

        System.out.println(a);
        //++a a = a + 1 执行这行代码之前,先自增,后给c赋值
        int c = ++a;

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        //幂运算 2^3 2*2*2=8 如何简单快速的计算2^3
        double pow = Math.pow(3,2);        //3的2c次方
        System.out.println(pow);
    }

if单选择

if(布尔表达式){
    //如果布尔表达式的值为true

if双选择结构

if(布尔表达式){
    //如果布尔表达式的值为true
}else{
    //如果布尔值表达式的值为false
}    

if多选择结构

if(布尔表达式 1){
    //如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
    //如果布尔表达式 2的值为ture执行代码
}else if(布尔表达式 3){
    //如果布尔表达式 3的值为ture执行代码
}else {
    //如果以上的布尔表达式都不为ture执行代码
} 

switch多选择结构

swith (expression){
    case value:
    //语句
    break;//可选
    //你可以有任意数量的case语句
    default://可选
    	//可选
}

Scanner对象

Scanner s = new Scanner(System.in);

while循环

package com.lol.LiOu.base;

public class WhileDemo03 {
    public static void main(String[] args) {
        //计算1+2+.....+100=?

        int i = 0;
        int sum = 0;

        while (i<=100){
            sum = sum + i;
                    i++;

        }
        System.out.println(sum);
    }
}
package com.lol.LiOu.base;

public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环  避免!!!
        while (true){
            //等待客户端连接
            //定时检查......
        }
    }
}
package com.lol.LiOu.base;

public class whileDemo01 {
    public static void main(String[] args) {
        //输出1-100

        int i = 0;

        while (i<100){
            i++;
            System.out.println(i);
        }
    }
}
posted @ 2020-11-09 14:01  JAVA初当力  阅读(84)  评论(0)    收藏  举报