0

## 【左京淳的JAVA學習筆記】第三章 運算子與分歧文

### 運算子

``````class Sample3_1 {
public static void main(String[] args) {
System.out.println(10 + 3);
System.out.println(10 - 3);
System.out.println(10 * 3);
System.out.println(10 / 3);
System.out.println(10 / 3.0);
System.out.println(10 % 3);
}
}
``````

### 延伸1-單項運算子

``````class Sample3_2 {
public static void main(String[] args) {
int a = 10;
int b = a++;
System.out.println(b);
a = 10;
int c = ++a;
System.out.println(c);
}
}
``````

a++與++a是什麼意思呢?
a++表示先把a的值代入b裡面之後，再把a的值+1。
++a表示先把a的值+1，再代入c裡面。

### 延伸2-關係運算子

``````class Sample3_3 {
public static void main(String[] args) {
int a = 10;int b = 20;
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a == b);
System.out.println(a != b);
}
}
``````

### 延伸3-邏輯運算子

``````class Sample3_4 {
public static void main(String[] args) {
int a = 10;int b = 10;int c = 10;int d = 10;
boolean result1 = a++ > 10 & ++b > 10;
System.out.println("result1:" + result1 + " a:" + a + " b:" + b);
boolean result2 = c++ > 10 && ++d > 10;
System.out.println("result2:" + result2 + " c:" + c + " d:" + d);
}
}
``````

``````result1:false a:11 b:11
result2:false c:11 d:10
``````

++b被執行了,而++d沒有被執行。

### 文字列處理

JAVA提供String及StringBuilder兩個類(class)來協助處理文字。

``````class Sample3_5 {
public static void main(String[] args) {
String s = "abcdefa";
System.out.println("charAt()     : " + s.charAt(2));
System.out.println("endsWith()    : " + s.endsWith("fa"));
System.out.println("indexOf()    : " + s.indexOf('c'));
System.out.println("replace()    : " + s.replace('a','z'));
System.out.println("substring()  : " + s.substring(2));
System.out.println("toUpperCase(): " + s.toUpperCase());
}
}
``````

``````charAt()     : c
endsWith()    : true
indexOf()    : 2
replace()    : zbcdefz
substring()  : cdefa
toUpperCase(): ABCDEFA
``````

``````class Sample3_6 {
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder("Ab Cdefge");
System.out.println("append()     : " + sb1.append("XYZ"));
StringBuilder sb2 = new StringBuilder("Ab Cdefge");
System.out.println("insert()     : " + sb2.insert(2,"ZZ"));
StringBuilder sb3 = new StringBuilder("Ab Cdefge");
System.out.println("reverse()     : " + sb3.reverse());
StringBuilder sb4 = new StringBuilder("Ab Cdefge");
sb4.setCharAt(3,'Z');
System.out.println("setCharAt()     : " + sb4);
StringBuilder sb5 = new StringBuilder("Ab Cdefge");
System.out.println("delete()     : " + sb5.delete(0,5));
StringBuilder sb6 = new StringBuilder("Ab Cdefge");
System.out.println("replace()     : " + sb6.replace(3,sb6.length(),"YYYY"));
StringBuilder sb7 = new StringBuilder("Ab Cdefge");
System.out.println("substring()     : " + sb7.substring(5,7));
StringBuilder sb8 = new StringBuilder("Ab Cdefge");
System.out.println("substring()     : " + sb8.substring(5));
}
}
``````

``````append()     : Ab CdefgeXYZ
insert()     : AbZZ Cdefge
reverse()     : egfedC bA
setCharAt()     : Ab Zdefge
delete()     : efge
replace()     : Ab YYYY
substring()     : ef
substring()     : efge
``````

### Data的比較

1. 基本型data的比較
2. 物件的比較

``````int i = 10; double b = 10.0;
System.out.println(i == b);
``````

``````class Sample3_7 {
public static void main(String[] args) {
int[] a1 = {10}; int[] a2 = {10}; int[] a3 = a1;
System.out.println("a1 == a2     : " + (a1 == a2));
System.out.println("a1 == a3     : " + (a1 == a3));
}
}
``````

``````a1 == a2     : false
a1 == a3     : true
``````

``````class Sample3_8 {
public static void main(String[] args) {
String s1 = new String("Tom");
String s2 = new String("Tom");
System.out.println("s1 == s2     : " + (s1 == s2));
System.out.println("s1.equals(s2): " + (s1.equals(s2)));
}
}
``````

``````s1 == s2     : false
s1.equals(s2): true
``````

``````class Sample3_9 {
public static void main(String[] args) {
String s1 = "Tom"; String s2 = "Tom";
System.out.println("s1 == s2     : " + (s1 == s2));
System.out.println("s1.equals(s2): " + (s1.equals(s2)));
}
}
``````

``````s1 == s2     : true
s1.equals(s2): true
``````

### if條件式

if(條件){

}else if(條件2){

}else{

}

if的部分比較單純，這邊就先跳過，需要的人請拜一下估狗大神。

### 條件運算子

``````class Sample3_10 {
public static void main(String[] args) {
int num = 20;
String str = "num的值為: ";
str += num < 10 ? "不到10" : "10以上";  // "+="意指把兩個文字列結合之後再覆蓋掉原先的文字列。
System.out.println(str);
}
}
``````

``````num的值為: 10以上
``````

### switch文

switch(比較對象){
case 值1:

case 值2:

default:

}

``````class Sample3_11 {
public static void main(String[] args) {
int num = 2;
switch(num){
case 1:
System.out.println("num = 1");
break;
case 2:
System.out.println("num = 2");
case 3:
System.out.println("num = 3");
default:
System.out.println("num為1,2,3以外的數值");
}
}
}
``````

``````num = 2
num = 3
num為1,2,3以外的數值
``````

switch迴圈在使用時要注意，每個處理後面都需加一行break;，

default是當所有條件都不符合時，會執行的處理。