51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

小游戏demo-java GUI-基础语法·运算符

>之前学习过Python、Java和C,所以很多运算符已经会了,这里只会补充自己不懂的

##运算符表>两个int值相除,可能会四舍五入

##运算符对数据类型的影响

long a = 1231231231231;
int b = 123;
short c = 10;
byte d = 8;

System.out.println(a+b+c+d); //Long 有比int级别更高的类型则转换为更高的类型
System.out.println(b+c+d); //Int
System.out.println(c+d); //Int 但即使没有int类型数据,运算后也会强制转换为int型

##a++ vs. ++a >再强化一下对自增 &自减的理解

//理解
int a = 3;
int b = a++; //执行完这行代码后,先给b赋值,下一次调用a时 a才会自增
System.out.println(b);//the output is 3
System.out.println(a); //the output is 4
System.out.println(b); //the output is 3
int c = ++a; //执行完这行代码后,a先自增,再给c赋值
System.out.println(c); //the output is 4
System.out.println(a); //the output is 4
//记忆
      //先赋值,再自增
int b = a++;
int c = ++a;
     //先自增,再赋值

##幂运算→工具类 >** Math.pow(a,x)** $$a^x$$

double pow = Math.pow(2,3);
Sytem.out.println(pow);

##逻辑运算符→短路运算 >如果在第一个判定即得出结果,则不会执行第二个判断(被截住了)

int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d); //This output is false
System.out.println(c); //This output is 5, which means c doesn't increment(自增)
//"c<4" is false, so "(c<4)&&(c++<4)" must be false
//we've already got the answer, so "(c++<4)" won't be executed

##!!位运算 >以0 1 为单位进行运算、输出

##字符串连接符:"" + (String) >""+ 会将+后的 转换为String

int a = 6;
int b = 19;
System.out.println(""+a+b); //printout 619
System.out.println(a+b+""); //printout 25

##三元运算符 x ? y : z >if x == true, the result is y, or else the result is z

String type = score <60 ?"不及格 ";"及格"
                  //  x ?   y    ;    z
赞(0)
未经允许不得转载:工具盒子 » 小游戏demo-java GUI-基础语法·运算符