1. 程式人生 > 資訊 >首個歐洲盃官方支付寶小程式報告出爐:中國球迷最愛 C 羅

首個歐洲盃官方支付寶小程式報告出爐:中國球迷最愛 C 羅

運算子

  • 優先順序 ()

  • 算術運算子:+、-、*、/、%(取餘數如2.....1取1)、++(自增)、--(自減)

  • 賦值運算子:=

  • 關係運算符:>、<、>=、<=、==、!=(不等於)、instanceof

  • 邏輯運算子:&&(與)、||(或)、!(非,不是..就是)

  • 位運算子:&、|、^、~、>>、<<、>>>

  • 條件運算子 ?:

  • 擴張賦值運算子:+=、-=、*=、/=

package operator;

public class Deom01 {
public static void main(String[] args) {
// 二元運算子
//ctrl + D :複製當前行到下一行
int a = 10;
int b = 20;
int c = 25;
int d = 30;

System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println((double)a/b);

long A = 123456789L;
int B = 123;
short C = 12;
byte D = 1;

System.out.println(A+B+C+D);//long
System.out.println(B+C+D);//int
System.out.println(C+D);//int
//有long結果都是long型別,有double都是double型別


//一元運算子 ++ 自增 -- 自減
int a1 = 3;
int b1 = a1++;
//執行完程式碼先給b1賦值,再自增(就是給a1加了,b1自己不加)
int c1 = ++a1;
//先自增再給c1賦值(給a1自己加了然後把數值給c1)

System.out.println(a1);
System.out.println(b1);
System.out.println(c1);

//冪運算 很多運算,會使用一些工具類來操作
double E = Math.pow(5,6);//5的6次方
System.out.println(E);

}

}
package operator;

//邏輯運算子
public class Demo02 {
public static void main(String[] args) {
//與(&&) 或(||) 取反(!)
boolean a = true;
boolean b = false;

System.out.println("a && b:"+(a&&b));//邏輯與運算:兩個變數都為真,結果才為true
System.out.println("a || b:"+(a||b));//邏輯或運算:兩個變數有一個為真,結果為true
System.out.println("!(a && b):"+!(a&&b));//如果為true,則變為flase,如果為flase則為true

//短路運算
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);

//位運算
/*
A = 0011 1100
B = 0000 1101
---------------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~B = 1111 0010
~A = 1100 0011

2*8 = 16 2*2*2*2
效率極高!!!
<< *2
>> /2

0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(2<<3);//2右移三位(0000 0010)-> (0001 0000)
//(2*3個2=2*2*2*2=16)

//拓展運算子 += -=
int e = 10;
int f = 20;

e+=f;//e = e+f
e-=f;//e = e-f

System.out.println(e);

//字串連線符 + , 前面有string型別會把其他運算元轉化為string進行連線
System.out.println(e+f);//=30
System.out.println(""+e+f);//=1020
System.out.println(e+f+"");//=30

//三元運算子 ? :
//x ? y : z
//如果x==true,則結果為y,否則為z

int score = 80;
String type = score <60 ?"不及格":"及格";//必須掌握
//if
System.out.println(type);

}
}