一,运算符

--1,算数运算符: +-*/ ++ -- %(模)
--2,比较运算符(结果是boolean类型):== != 
--3,逻辑运算符(结果是boolean类型):+ !(取反) & |
--4,三元运算符: 1 ? 2 : 3
        --结果是2或者是3,到底是2还是3要看1的结果了,1如果描述的成立,结果是2.1如果描述不成立,结果是3.
--5,赋值运算符: = += -= *= /=
--6,逻辑运算符 & | 
    --逻辑&:表示并且关系
        1 & 2 :如果最终运算的结果,想要是true,要求1和2,必须都是true
    --短路与/双与&&:表示并且关系 -- 高效
        1 && 2 :
            2的位置可能会发生短路,当1的位置判断完得到false时,结果就已经注定了是false,此时2可以不参与运算(发生短路).可以提高效率.
        
    --逻辑|:表示或者关系
        1 | 2 :如果最终运算的结果,想要是true,要求1和2中,有一个true就行
    --短路或/双或||:表示或者关系  -- 高效    
        1 || 2 :
            2的位置可能会发生短路,当1的位置判断完得到true时,结果早都注定了是true,此时2可以不参与运算(发生短路).用来提高效率.
            --7,练习:自增自减    
    package cn.tedu.basic;
    //这个类用来测试 自增++   自减--
    public class Test1_ZIzeng {
        public static void main(String[] args) {
            int a = 1;
            //符号在后,先使用再变化
            System.out.println(a++);//1
            
            int b = 1;
            //符号在前,先变化再使用
            System.out.println(++b);//2
            
            System.out.println(++b+b+a++);//3+3+2=8
            
            int c = 1;
            //符号在后,先使用再变化
            System.out.println(c--);//1
            
            int d = 1;
            //符号在前,先变化再使用
            System.out.println(--d);//0
            System.out.println(--c-c-d--);//-1-(-1)-0=0
        }
    }
 //这个类用来测试 三元运算符
    public class Test2_MaxValue {
        public static void main(String[] args) {
            int a = new Scanner(System.in).nextInt();
            int b = new Scanner(System.in).nextInt();

            // 两个数里的大值:max记录的值可能是a也可能是b,到底是a还是b呢?--要看a>b判断成立不
            int max = a > b ? a : b;
            System.out.println("a b里的大值是:" + max);

            // 三个数里的大值:
            int c = new Scanner(System.in).nextInt();
            
            //res记录着max和c里的大值
            int res = max  > c ? max : c ;
            System.out.println("a b c里的大值是:"+res);
            
            //TODO 优化:一步到位
    //        int res = a > b ? 2 : 3 ;
    //        int res = a > b ? a大 : b大 ;
            int res = a > b ? ( a > c ? a : c ) : ( b > c ? b : c ) ;
            System.out.println("a b c里的大值是:"+res);
        }
    }
    
    二,!!!分支结构:if
--1,概述
    分支结构是相对于顺序结构而言的.顺序结构只能一行一行顺序的从上往下执行.但是无法完成先判断再执行的需求.
--2,语法
    单分支:
    if(判断条件){
        满足了条件代码
    }
    多分支:
    if(判断条件){
        满足了条件代码
    }else{
        不满足了条件代码
    }
    嵌套分支:
    if(判断条件1){
        满足了条件代码1
    }else if(判断条件2){
        满足了条件代码2
    }else if(判断条件3){
        满足了条件代码3
    }else{
        上面的谁都不满足才要执行的代码4
         package cn.tedu.ifdemo;
        import java.util.Scanner;
        //这个类用来测试 if
        public class Test4_IF {
            public static void main(String[] args) {
                //1,接收用户输入的原价
                double price = new Scanner(System.in).nextDouble();
                
        //        double discount = price;//定义变量,记录折后价
                
                //2,开始计算折后价,并输出
                if(price >= 5000) {//满5000打5折
        //            price = price * 0.5 ;
                    
                    price *= 0.5 ;//等效于:price=price*0.5;相当于是一种简写形式.
                    
                }else if(price >= 2000) {//满2000打8折
        //            price = price * 0.8 ;
                    price *= 0.8 ;
                }else if(price >= 1000) {//满1000打9折
        //            price = price * 0.9 ;
                    price *= 0.9 ;
                }
                //3,输出
        //        System.out.println("原价是:"+price+",折后价是:"+discount);
                System.out.println("折后价是:"+price);
            }
        }   
     --4,练习:统计学员得分
    package cn.tedu.ifdemo;

    import java.util.Scanner;

    //这个类用来测试 if
    public class Test5_If2 {
        public static void main(String[] args) {
            // 1,接收用户输入的分数
            int score = new Scanner(System.in).nextInt();

            // 为了增强程序的健壮性.设置上限100和下限0
            if (score > 100 || score < 0) {
                System.out.println("请您输入有效的分数!");
                return;// 结束程序
            }
            // 2,判断
            if (score >= 90) {// 90分以上 优秀
                System.out.println("优秀");
            } else if (score >= 80 && score <= 89) {// 80~89 良好
                System.out.println("良好");
            } else if (score >= 70 && score <= 79) {// 70~79 中等
                System.out.println("中等");
            } else if (score >= 60 && score <= 69) {// 60~69 及格
                System.out.println("及格");
            } else if (score < 60) {// 60分以下 不及格
                System.out.println("不及格");
            }
        }
    }
    
    三,分支结构:switch
--1,概述
    也可以完成先判断再执行的需求.
--2,语法:
    switch(判断条件){
        case 0 : syso(0) ;    break;
        case 1 : syso(1) ;    
        case 'x' : syso(2) ;    
        case "java" : syso(3) ; 

        default:syso(100) ;    
    }
--3,练习:
    package cn.tedu.ifdemo;
    //这个类用来测试  switch
    public class Test6_Switch {
        public static void main(String[] args) {
            
            int sum  = 2 ;
            //1,判断条件:可以转化为int类型的表达式.
            //可以是byte short char int类型.jdk7以后,支持String类型.
            switch(sum) {
                case 1 : System.out.println(1);
                //2,自从成功匹配了case后,会继续向后穿透所有case包括default
                case 2 : System.out.println(2);break ;//3,立刻结束
                case '2' : System.out.println('2');break ;
                case 'x' : System.out.println('x');break ;
                case 100 : System.out.println(100);break ;
                
                default :System.out.println(666);
            }
        }
    }

四,循环结构:for

--1,概述
    是指在程序中,需要重复执行很多次的某些功能.
--2,语法
    for(循环的开始位置;循环的判断条件;循环的更改条件){循环体}
--3,练习:
    package cn.tedu.fordemo;
    //这个类用来测试 for循环
    public class Test7_For {
        public static void main(String[] args) {
            //int i = 0 循环的开始位置
            //i <= 10 循环的判断条件
            //i++ 循环的更改条件,使i不断自增
            //i 就表示每次获取到的数据
            for(int i = 0; i <= 10 ; i++ ){//练习:打印0到10
                System.out.println(i);//打印当前i的值
            }
            
            //练习:打印10到0
            for(int i = 10 ; i >= 0  ; i-- ) {
                System.out.println(i);
            }
            
            //TODO 打印8,88,888,8888
            //TODO 画流程图!!
        }
    }

东霓&西决&南音
13 声望0 粉丝

新人驾到,多多关照