第三章 Java符號相關


+  - * / 自不用說,

%求餘數

 ++  自增一,--   自減一;

(注意,自增,自減 出現在變量左邊,或者右邊,是不一樣的。如下)

例子1

Int  a=5;

Int  b=++a;//a先執行自增,完事賦值給b

System.out.println(a+a);

System.out.println(b+b);

運行結果:

a:6     b:6

例子2

 

 

package com.mooc;

 

public class mathcharacter01 {

public static void main(String[] args){

int a =12;

int  b=24;

int sum=a+b;

System.out.println("兩數之和="+sum);

}

 

}

 

 

續  第三章

例子2;

Int  a=5;

Int b=a++;

System.out.println(“a”+a);

System.out.println(“b”+b);

 

運行結果:

a:6

b:5

.

目前自加自減符號  只能用來操作變量,不能直接用來操作數和常量。

比如 寫法8++  6--  就是錯的。

 

package com.mooc;

 

public class mathcharacter02 {

public static void main(String[] args){

int age1=24;

int age2=18;

int  age3=36;

int  age4=27;

int sum1=age1+age2+age3+age4;

double  sum2=(age1+age2+age3+age4)/4;

int sum3=age1-age2;

int sum4=--age1;

System.out.println("年齡總和="+sum1);

System.out.println("平均年齡="+sum2);

System.out.println("年齡差值="+sum3);

System.out.println("自減後的年齡="+sum4);

 

}

}

 

 

3-3   賦值運算符

=    +=  -=  *=  /=  %=

 

package com.mooc;

 

public class mathcharacter03 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int one=10;

int two=20;

int three=0;

three=one +two;

three+=one;

three-=one;

three*=one;

three/=one;

three%=one;

System.out.println("three="+three);

 

}

 

}

 

3-4比較運算符

1.>大於   2. <小於

3.>=       4.<=

5.!=不等於   6.==等於

1-4 只支持 左右兩邊 操作數是 數值類型

5,6 兩邊操作數  既可以是數值,也可以是引用類型。

 

package com.mooc;

 

public class mathcharacter04 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=16;

double  b=9.5;

        String  str1="hello";

        String  str2="immoc";

        System.out.println("a等於b:"+(a==  b));

        System.out.println("a大於b:"+(a>  b));

        System.out.println("a大於等於b:"+(a>=  b));

        System.out.println("a小於b:"+(a<  b));

        System.out.println("a小於等於b:"+(a<=  b));

        System.out.println("str1等於str2:"+(str1==  str2));

}

 

}

 

 

3-5  邏輯運算符

&&   與  兩者真  ,纔算真。  

||    或  一個真  就是真

   

^   亦或  有且僅有一個真,纔是真(感覺比較拗口 難記誒)

有一種有趣的短路現象,

例子1one>two&&(one<three)

左邊是錯,右邊就沒有必要執行了。

例子2   (one<three)||  one>two

左邊對啦  就沒有必要執行右邊啦。

 

package com.mooc;

 

public class mathcharacter05 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

boolean a=true;

boolean b=false;

boolean c=false;

boolean d=true;

 System.out.println((a&&b)+"未通過");

 

 System.out.println((a||d)+"通過");

 System.out.println((!a)+"未通過");

 System.out.println((c^d)+"通過");

}

 

}

 

 

3-7  條件運算符

就是三元運算符(?:)

語法格式   :布爾表達式?表達式1:表達式2

運算過程 如果布爾表達式值爲TRUE,就返回表達式一,爲FALSE 就返回表達式2,

 

package com.mooc;

 

public class mathcharacter06 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int  score=68;

String mark=( score>=60)?"及格":"不及格";

System.out.println("考試成績如何:"+mark);

 

}

 

}

 

 

 

 

3-8  運算符號的優先級

反正沒有必要死記硬背,完事實際開發中 ,可以用小括號輔助 設置 優先級。

 

package com.mooc;

 

public class mathcharacter07 {

public static void main(String[] args) {

int m=5;

int  n=7;

int x=(m*8/(n+2))%m;

System.out.println("m:"+m);

System.out.println("n:"+n);

System.out.println("x:"+x);

}

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章