博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第3章 控制程序流程
阅读量:4659 次
发布时间:2019-06-09

本文共 5635 字,大约阅读时间需要 18 分钟。

3.1 使用java运算符

  3.1.1 优先级

  3.1.2 赋值

  3.1.3 算术运算符

  3.1.4 自动递增和递减

  3.1.5 关系运算符

  3.1.6 逻辑运算符

  3.1.7 按位运算符

  3.1.8 移位运算符

  3.1.9 三元if-else运算符

  3.1.10 逗号运算符

  3.1.11 字串运算符

  3.1.12 运算符常规操作规则

  3.1.13 造型运算符

  3.1.14 java没有”sizeof“

  3.1.15 复习计算顺序

  3.1.16 运算符总结

3.2 执行控制

  3.2.1 真和假

  3.2.2 if else

  3.2.3 反复

  3.2.4 do-while

  3.2.5 for

  3.2.6 中断和继续

  3.2.7 开关

 

3.1.1 优先级

package com.sunny.hello.c3; public class Demo1 {
public static void main(String[] args) {
int x = 1, y =1, z = 1; int a = x + y - 2/2 + z; int b = x + (y - 2)/(2 + z); System.out.println("a = " + a + " ,b = "+b); } }

输出:

a = 2 ,b = 1

 

3.1.2 赋值

package com.sunny.hello.c3;public class Assignment {    //理解基本类型和对象赋值的区别    public static void main(String[] args) {        Number n1 = new Number();        Number n2 = new Number();        n1.i = 9;        n2.i = 47;        System.out.println("1:n1.i: " + n1.i + ", n2.i: " + n2.i);        n2 = n1;        System.out.println("2:n1.i: " + n1.i + ", n2.i: " + n2.i);        n1.i = 27;        System.out.println("1:n1.i: " + n1.i + ", n2.i: " + n2.i);    }}class Number {    int i;}

运行结果:

1:n1.i: 9, n2.i: 472:n1.i: 9, n2.i: 91:n1.i: 27, n2.i: 27

 

package com.sunny.hello.c3;public class PassObject {    static void f(Letter y){        y.c = 'z';    }    public static void main(String[] args) {        Letter x = new Letter();        x.c = 'a';        System.out.println("x.c:"+x.c);        f(x);        System.out.println("x.c:"+x.c);    }}class Letter{    char c;}

运行结果:

x.c:ax.c:z

 

3.1.4 i++与i--

package com.sunny.hello.c3;public class AutoInc {    public static void main(String[] args) {        int i = 1;        System.out.println("i:"+i);        System.out.println("++i:" + ++i);        System.out.println("i++:" + i++);        System.out.println("i:" + i);        System.out.println("--i:" + --i);        System.out.println("i--:" + i--);        System.out.println("i:"+i);    }}

运行结果:

i:3--i:2i--:2i:1

 

3.1.5 关系运算符

package com.sunny.hello.c3;public class Equivalence {    public static void main(String[] args) {        //检查对象是否相等        Integer n1 = new Integer(47);        Integer n2 = new Integer(47);        System.out.println(n1 ==  n2);  //false        System.out.println(n1.equals(n2)); //true    }}
package com.sunny.hello.c3;public class EqualsMethod {    public static void main(String[] args) {        Value v1 = new Value();        Value v2 = new Value();        v1.i = 100;        v2.i = 100;        System.out.println(v1.equals(v2)); //false        /* 这里的equals调用的是Object类的        public boolean equals(Object obj) {            return (this == obj);        }*/    }}class Value{    int i;}

 

3.1.6 逻辑运算符

package com.sunny.hello.c3;public class ShortCircuit {    public static boolean test1(int i){        if(i>1){            System.out.println("test1()执行");           return true;        }        return false;    }    public static boolean test2(int i){        if(i%2 == 0){            System.out.println("test2()执行");            return true;        }        return false;    }    public static void main(String[] args) {        int a = 2, b = 6;        if(test1(a) && test2(b)){            System.out.println("未短路");        }else{            System.out.println("短路");        }    }}

运行结果:

test1()执行test2()执行未短路

若将a的值改为1,则:(注意,test1为false,则test2不会继续执行了,这就是短路)

短路

 

3.1.8 移位运算符

<<:左移

>> :右移(若值为正,则在高位插入 0;若值为负,则在高位插入 1)

>> :无符号右移(无论正负,都在高位插入0)

package com.sunny.hello.c3;public class URShift {    public static void main(String[] args) {        int a = 2;        System.out.println(a<<2);        System.out.println(a>>2);        System.out.println(a>>>2);        short b = -1;        System.out.println(b<<2);        System.out.println(b>>2);        System.out.println(b>>>2);    }}

运行结果:

800-4-11073741823

 

3.1.9 三元运算符  

public int ternary(int i){        return i<10 ? i*100 : i*10;    }

 

3.1.15 复习计算顺序

算术运算符>关系运算符>逻辑运算符>赋值

 

3.1.16 运算符总结

溢出问题:

package com.sunny.hello.c3;public class Overflow {    public static void main(String[] args) {        int big = Integer.MAX_VALUE;        System.out.println(big);        big = big * 4;        System.out.println(big);    }}

输出结果:

2147483647-4

 

3.2: 执行控制

break和continue区别:

package com.sunny.hello.c3;public class Demo {    public static void main(String[] args) {        for(int i=0; i<6; i++){            if(i%2 != 0){                break;            }else{                System.out.println(i);            }            System.out.println(i + "是偶数");        }    }}

运行结果:

00是偶数

 

package com.sunny.hello.c3;public class Demo {    public static void main(String[] args) {        for(int i=0; i<6; i++){            if(i%2 != 0){                continue;            }else{                System.out.println(i);            }            System.out.println(i + "是偶数");        }    }}

运行结果:

00是偶数22是偶数44是偶数

 

switch开关:

package com.sunny.hello.c3;public class VowelsAndConsonants {    public static void main(String[] args) {        for (int i = 0; i < 5; i++){            char c = (char)(Math.random()*26+'a');            System.out.println("c:"+c);            switch (c){                case 'a':                case 'e':                case 'i':                case 'o':                case 'u':                    System.out.println("vowel");                    break;                case 'y':                case 'w':                    System.out.println("Sometimes a vowel");                    break;                default:                    System.out.println("consonant");            }        }    }}

运行结果:

c:cconsonantc:hconsonantc:gconsonantc:evowelc:bconsonant

 

转载于:https://www.cnblogs.com/sunnyDream/p/8012431.html

你可能感兴趣的文章
centos7关闭防火墙
查看>>
《C#高级编程》 读书笔记 -索引
查看>>
session cookie原理及应用
查看>>
ID3算法详解
查看>>
BZOJ1925: [Sdoi2010]地精部落
查看>>
学习进度条第十一周
查看>>
linux常用命令
查看>>
设置SQL PLUS环境
查看>>
关于虚拟机VM
查看>>
eclipse、tomca和jvm的相关内存配置
查看>>
python的迭代器
查看>>
spy memcached spring demo
查看>>
Python基础语法
查看>>
4.1.1 硬币游戏
查看>>
获取服务器信息
查看>>
JavaScript_5_对象
查看>>
DataGrip导出查询结果数据
查看>>
2019春第三次实验报告
查看>>
DockerToolbox在Win7上的安装和设置
查看>>
【洛谷 1168】动态中位数
查看>>