美文网首页
2018-08-23

2018-08-23

作者: Retree | 来源:发表于2018-08-23 10:24 被阅读0次

三元运算符

int i = 2;
System.out.println(i<3?i*10:i*20)
如果i<3为真,执行i*10,如果为假,执行i*20
格式就是“判断式?表达式1:表达式2”

数字与字符串的连接
数字在前,字符串在后,那么数字先进行运算,再与字符串拼接
字符串在前,数字在后,直接拼接,数字部分不进行运算

public class test1{ 
    public static void main(String[] args){
        int x = 0, y = 1, z = 2;
        System.out.println(x+y+z+"hello");
        System.out.println("hello"+x+y+z);
    }
}
image.png

类型转换,默认是向上转换,以免丢失精度,比如int+long,结果类型就是long。也允许向下转换,必须标明,不建议向下转换,你都不知道会怎么给你丢失

public class test1{ 
    public static void main(String[] args){
        int x = 1;
        long y = 20000;
        int z = x+y ;
        System.out.println(z);
    }
}
这时编译提示你可能精度丢失

可以这么改
    public class test1{ 
        public static void main(String[] args){
            int x = 1;
            long y = 20000000;
            int z = (int)(x+y);
            System.out.println(z);
        }
    }

break,判断条件为true时,中断,退出循环
continue,判断条件为true时,执行循环体内后面的语句,否则继续回到开始位置循环,

    public class test1{ 
        public static void main(String[] args){
            int i = 0;
            out:
            while( i < 101){
                System.out.println(i);
                if(i==47){
                    break out;
                    }
                i++;            
            }
        }
    }


或者使用return
    public class test1{ 
        public static void main(String[] args){
            int i = 0;
            out:
            while( i < 101){
                System.out.println(i);  
                if(i==47){
                    return;
                    }
                i++;            
            }
        }
    }

嵌套循环需要使用到标签,不然break只是中断了内部循环,无法中断外部循环
switch的选择因子必须是整数(int,char这样的),浮点数和字符串不会在switch里生效。

相关文章

  • 设立具体的目标

    2018-08-23 戴师傅 2018-08-23 20:32 打开App (稻盛哲学学习会)打卡第120天 姓名...

  • 去掉烂模式,从自己做起

    幸福时刻(173)2018.8.23 利花花 关注 2018-08-23 11:20 · 字数 2049 · 阅读...

  • Day11 #100DaysofMLCoding#

    2018-08-23 24https://github.com/hse-aml/intro-to-dl以及如何用c...

  • 懂你 L4-U1-1-Dialogue

    流利说 D57 2018-08-23 四 一、复习 Level4-Unit1-Part1*Learning- Vo...

  • 2018-08-23

    《如何学习》四 41晓春 晓春的蜗居 2018-08-23 07:19 · 字数 412 · 阅读 0 · 日记本...

  • 设立具体的目标

    2018-08-23 (稻盛哲学学习会)打卡第101天 姓名:祝新华 部门:业务部 组别:待定 【知~学习】...

  • 设立具体的目标

    2018-08-23 (稻盛哲学学习会)打卡第140天 姓名:王燕君 部门:分水碶 组别:利他三组 【知~学习】 ...

  • Android集成友盟QQ分享闪退,IllegalStateEx

    问题: 集成友盟,分享到QQ,出现闪退。 手机:华为P10,操作系统:安卓 8.0 | 2018-08-23 22...

  • 2018-08-23

    2018-08-23 事件:今天单位同事一起聚餐。 感受:开心,感恩,感谢。 想法:这几年和大家在一起很开心,感恩...

  • 2018-08-23推广恩师课程

    2018-08-23推广恩师--小巫老师,小巫养育学堂的课程。非常感恩平台给到我的支持。感恩这份美好的相遇,越成长...

网友评论

      本文标题:2018-08-23

      本文链接:https://www.haomeiwen.com/subject/ovrpiftx.html