(十)java条件结构

  • 条件结构
    • if(条件表达式) {};
    • if(条件表达式){} else {};
    • if(条件表达式){} else if(条件表达式) {} else if(条件表达式){} ...... else{}
  • switch结构
    • switch(需要进行比较的元素) {
                case 条件:
                    语句;
                    break;
                case 条件:
                    语句;
                    break;
                ......
            default:
                语句;
                break;
            };
import java.util.Scanner;
class  Tjjg
{
    /*
    *java条件结构
    *if或else下面有多条语句时,需要用大括号包起来。
    * else总是和离她最近的一个if或else if有关系
    */
    public static void main(String[] args)
    {
        int math = 97;
        if(math > 90) {
            System.out.println(" Good!");
        } else if(math >95) {
            System.out.println("Very Good");
        } else if(math < 90) {
            System.out.println("加油!!!");
        } else {
            return ;
        }
 
        //根据输入的月份显示哪个季节
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入月份");
        int a = sc.nextInt();
        switch (a) {
        case 3:
        case 4:
        case 5:
            System.out.println("a春季");
            break;
        case 6:
        case 7:
        case 8:
            System.out.println("夏季");
            break;
        case 9:
        case 10:
        case 11:
            System.out.println("秋季");
            break;
        case 12:
        case 1:
        case 2:
            System.out.println("冬季");
            break;
        default:
            System.out.println("输入月份无效");
        }
    }
}

  

posted @ 2018-07-12 17:03  狗尾草的博客  阅读(622)  评论(0编辑  收藏  举报