JavaSE之流程控制语句

单一条件 if

public class Demo1 {
    public static void main(String[] args) {
        int i = 0;
        int a = i++;
        if(i>=0){
            a++;
            System.out.println(i); 
        }
        System.out.println(a);    
    }
}

二选一 if....else

public class Demo3 {
    public static void main(String[] args) {
        int score = 99;
        if(score >= 60) {
            System.out.println("及格");
        }
        else {
            System.out.println("不及格");
        }
    }
}
public class Demo2 {
    public static void main(String[] args) {
        // 有一个数字为45327,判断该数字是否能被13整除,是否能被17整除。
        int num = 45327;    
        if(num % 13 == 0) {
            System.out.println("能被13整除");
        }else {
            System.out.println("不能被13整除");
        }
        
        if(num % 17 == 0) {
            System.out.println("能被17整除");
        }
        else {
            System.out.println("不能被17整除");
        }
    }
}

多选一 if....else if....else

public class Demo6 {
    public static void main(String[] args) {
        // if..else if..[else]
        int score = 56;
        if(score >= 90 && score <= 100) {
            System.out.println("A");
        }else if(score >= 80 && score < 90) {
            System.out.println("B");
        }else if(score >= 70 && score < 80) {
            System.out.println("C");
        }else if(score >= 60 && score < 70) {
            System.out.println("D");
        }else if(score >= 0 && score < 60) {
            System.out.println("E");
        }else{
            System.out.println("分数无效");
        }
    }
}

变量的作用域

public class Demo7 {
    public static void main(String[] args) {
        // 作用域(有效范围) : 从声明的位置开始,到它所在的块结束
        // 同一个块内不能声明同名变量(作用域不能有交集)
        int num = 100;
        if(num > 50) {
            int i = 10;
            System.out.println(i);
        }
        int i = 900;
        System.out.println(i);
    }
}

switch...case

public class Demo8 {
    public static void main(String[] args) {
        int num = 99;
//      switch后的这个变量或表达式,会找到第一个匹配的选项case,执行以后所有switch中的代码
//      case 后的值不能有相同的
//      break终止当前块的运行
//      default相当于else
//      注意:避免重复代码
        switch(num) {
            case 100:
            case 99:
            case 98:
                System.out.println("满分");
                break;
            case 97:
                System.out.println("差3分");
                break;
            case 96:
                System.out.println("差4分");
                break;
            default:
                System.out.println("java2");
        }
    }
}
public class Demo9 {
    public static void main(String[] args) {
//      JDK7之前,switch(参数) 只支持  byte short char int + String
//      JDK7之后,支持了String类型
        String str = "hello";
        switch(str) {
            case "hello":
                System.out.println(123);
        }
    }
}

if小陷阱

public class Demo13 {
    public static void main(String[] args) {
        int a = 100;
        // 注意;if和else如果没有写{},只影响第一行代码
        if(a < 50)
        System.out.println("大于50");
        // 注意:这里有一个;代表结束
        if(a < 50);
        // 相当于if(a < 50){}
        System.out.println("大于50");
    }
}

避免被坑练习

public class Practice4 {
    public static void main(String[] args) {
        // 1
//      int x = 20;
//      int y = 30;
//      if(x > 10 && y == y++) 
//      System.out.println(y);
//      System.out.println(x);
//      System.out.println(y);
//      3个数字 31 20 31
        
        // 2
//      int x = 20;
//      int y = 30;
//      if(x > 100 && y == y++) 
//      System.out.println(y);
//      System.out.println(x);
//      System.out.println(y);
//      2个数字 20 30
        
        // 3
//      int x = 20;
//      int y = 30;
//      if(x > 10 || y == y++)
//      System.out.println(y);
//      System.out.println(x);
//      System.out.println(y);
//      3个数字 30 20 30
        
        // 4
//      int x = 20;
//      int y = 30;
//      if(x > 10 && y == y++);
//      System.out.println(y);
//      System.out.println(x);
//      System.out.println(y);
//      3个数字 31 20 31
        
        // 5
//      int x = 20;
//      int y = 30;
//      if(x > 10 || y == y++);
//      System.out.println(y);
//      System.out.println(x);
//      System.out.println(y);
//      3个数字 30 20 30
    }
}

字符串反转

public class HomeWork_OP8 {
    public static void main(String[] args) {
        // 由命令行输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321 
        // 方法1
        int num = 9876;
//      int q = num/1000;
//      int b = num/100%10;
//      int s = num/10%10;
//      int g = num%10;
//      System.out.println(g+""+s+b+q);
//      System.out.println(g*1000+s*100+b*10+q);
        
        // 方法2(以后再说)
        StringBuffer sbf = new StringBuffer(num+"");
        System.out.println(sbf.reverse());
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_34218890/article/details/88280068