zoukankan      html  css  js  c++  java
  • JAVA基础教程day03--运算符

    一、算术运算符

    1. %(取余)、前++(先运算后取值)、后++(先取值后运算)、前--(先运算后取值)、后--(先取值后运算)

        (1): 5%2=1

        (2):前++

    //前++
    int a1=10;
    int b1=++a1;
    System.out.println(b1);//b1=11

        (3):后++

    //后++
    int a2=-10;
    int b2=a2++;
    System.out.println("a2="+a2+",b2="+b2);//b2=10
    

        (4): 前--

    int a1=20;
    int a2=--a1;
    System.out.println(a2);//a2=19    

        (5):后--

    int a1=20;
    int a2=a1--;
    System.out.println(a2);//a2=20
    

      

    二、赋值运算符

    //思考1
    short s = 3; 
    s = s+2; ①
    s += 2; ②//运算后保持原来的数据类型;
    两者有什么区别
    //思考2
    int i = 1;
    i *= 0.1;
    System.out.println(i);//
    i++;
    System.out.println(i);//
    //思考3:
    int m = 2;
    int n = 3;
    n *= m++; 
    System.out.println("m=" + m);
    System.out.println("n=" + n);
    //思考4:
    int n = 10;
    n += (n++) + (++n);
    System.out.println(n);
    

      

      

    三、比较运算符

    运算符     运算       范例             结果
    ==         相等于    4==3          false
    !=          不等于    4!=3            true
    <           小于       4<3             false
    >            大于      4>3             true
    <=         小于等于 4<=3          false
    >=         大于等于 4>=3          true
    instanceof 检查是否是类的对象 “Hello” instanceof String true                
    

      

    四、逻辑运算符

    &,&&,|,||,^

    //&
    x>10 &x<20 两边都要满足类似且的意思
    //&&
    x>10 && x<20 当左边为真时有边要参与计算,如果左边为假右边不参与计算。
    

      

    |
    x>100 | x>50  //类似或者的意思满足其中一个条件
    ||
    x>100 | x>50  //如果左边为真,右边不参与计算,如果左边为假右边参与计算。
    

      

    ^
    两边要不同
    

      

    //练习1
    int x = 1;
    int y=1;
    if(x++==2 & ++y==2){
    x =7;
    }
    System.out.println("x="+x+",y="+y);
    //练习2
    int x = 1,y = 1;
    if(x++==2 && ++y==2){
    x =7;
    }
    System.out.println("x="+x+",y="+y);
    //练习3
    int x = 1,y = 1;
    if(x++==1 | ++y==1){
    x =7;
    }
    System.out.println("x="+x+",y="+y);
    练习4//
    int x = 1,y = 1;
    if(x++==1 || ++y==1){
    x =7;
    }
    System.out.println("x="+x+",y="+y);
    

      

    五、位运算符

    六、三元运算符

    (x>10)? a:b
    //如果x>10 x=a,否则x=b
    //练习 int a1=30; int a2=40; int a3=50; int max1=(a1>a2) ? a1:a2; int max2=(max1>a3) ? max1 :a3;

    七、流程控制

    1、顺序结构

    2、分支结构

    (1)if-else

    public class AgeTest{
    public static void main(String args[]){
    int age = 75;
    if (age< 0) {
    System.out.println("不可能!");
    } else if (age>250) {
    System.out.println("是个妖怪!");
    } else {
    System.out.println(“人家芳龄 " + age +" ,马马乎乎啦!");
    } } }
    

    (2)

    switch-case
    String season = "summer";
    switch (season) {
    case "spring":
        System.out.println("春暖花开");
        break;
    case "summer":
        System.out.println("夏日炎炎");
        break;
    case "autumn":
        System.out.println("秋高气爽");
    break;
        case "winter":
        System.out.println("冬雪皑皑");
    break;
        default:
        System.out.println("季节输入有误");
    break; }
    

      

    3、

  • 相关阅读:
    Scoop Windows 的命令行安装程序管理工具
    微信小程序:用 Promise 解决方案代替回调地狱。 修复 this._invokeMethod is not a function 的问题
    微信小程序 获取地理位置信息
    Vue 与 动态组件 import 的尝试
    exception: TypeError: Cannot read property '_modulesNamespaceMap' of undefined at getModuleByNamespac
    Vue 项目中断点没有跳转到指定源码的问题
    检查 chrome 插件是否存在
    vue-devtools 获取到 vuex store 和 Vue 实例的?
    HTML5的服务器EventSource(server-sent event)发送事件
    ps 渐进式图片的技巧(支持jpg,gif,png)
  • 原文地址:https://www.cnblogs.com/snackpython/p/11783596.html
Copyright © 2011-2022 走看看