九、Java語句

語句:
條件語句-根據不一樣的條件執行不一樣的語句。
if
if .. else
if .. else if
if .. else if .. else if ..else
只有一句須要執行的語句時,能夠省略{}java

TestIF.java
public class TestIF {
public static void main (String[] args) {
int i = 20;
if (i < 20) {
System.out.println("<20");
System.out.println("<20");
} else if (i < 40) {
System.out.println("<40");
} else if (i < 60) {
System.out.println("<60");
}else
System.out.println(">=60"); //只有一句須要執行的語句時,能夠省略{}
}
}
建議即便有一句語句時也是用大括號

switch
循環語句-重複執行某些動做
for循環語句
for循環語句爲以下形式:
for(表達式1;表達式2;表達式3){ 語句;... ; }
執行過程
首先計算表達式1,接着執行表達式2,若表達式2的值爲true,則執行語句,接着計算表達式3,再判斷表達式2的值;依次重複下去,直到表達式2的值爲false
for語句中三個表達式均可以省略spa

TestFor.java
public class TestFor {
public static void main (String[] args) {
long result = 0;
long f = 1;
for (int i = 1; i <= 10; i++) {
f = f * i;
result += f;
}
System.out.println("result=" +result);
}
}
C:\Users\root\Desktop>javac TestFor.java

C:\Users\root\Desktop>java TestFor
result=4037913

 

OddSum.java
public class OddSum {
  public static void main (String[] args) {
    long result = 0;
    for (int i=1; i<=99; i+=2) {
      result += i;
    }
    System.out.println("result=" + result);
  }
}
C:\Users\root\Desktop>javac OddSum.java

C:\Users\root\Desktop>java OddSum
result=2500

 

while 語句爲以下形式:
while(邏輯表達式){語句; ... ;}
執行過程
先判斷邏輯表達式的值,若=true則執行其後面的語句,而後再判斷條件,知道條件不成立
      語句
表達式-》       -》結束
do .. while語句形式以下:
do{語句;... ;}while(邏輯表達式);
執行過程
先執行語句,再判斷邏輯表達式的值,若爲true,再執行語句,不然結束循環。
        表達式
語句-》          -》結束blog

TestWhile.java
public class TestWhile {
  public static void main (String[] args) {
    int i = 0;
    while (i<10) {
      System.out.println(i);
      i++;
    } {
      System.out.println("-----------------------------------");
    }
    i = 0;
    do {
      System.out.println(i);
      i++;
    } while (i<10);
  }
}

C:\Users\root\Desktop>javac TestWhile.java

C:\Users\root\Desktop>java TestWhile
0
1
2
3
4
5
6
7
8
9
-----------------------------------
0
1
2
3
4
5
6
7
8
9

break & continue 語句
break語句用於終止某個語句塊的執行,用在循環語句體中,能夠強制退出循環;例如:ip

TestBreak.java
public class TestBreak {
  public static void main (String[] args) {
    int stop = 4;
    for(int i = 1; i < 10; i++) {
      //if (i == stop) break;
      System.out.println("i=" + i);
    }
  }
}
C:\Users\root\Desktop>javac TestBreak.java

C:\Users\root\Desktop>java TestBreak
i=1
i=2
i=3
TestBreak.java
public class TestBreak {
  public static void main (String[] args) {
    int skip = 4;
    for(int i = 1; i < 10; i++) {
      if (i == skit) continue;
      System.out.println("i=" + i);
    }
  }
}
C:\Users\root\Desktop>javac TestBreak.java

C:\Users\root\Desktop>java TestBreak
i=1
i=2
i=3
i=5
i=6
i=7
i=8
i=9
100之內前5個能夠被3整除的數
TestWhile2.java
public class TestWhile2 {
  public static void main(String[] args) {
    int num = 0, i = 1;
    while (i <= 100) {
      if (i % 3 == 0) {
        System.out.println(i + " ");
        num++;
      }
      if (num == 5) {
        break;
      }
      i++;
    }
  }
}

C:\Users\root\Desktop>javac TestWhile2.java

C:\Users\root\Desktop>java TestWhile2
3
6
9
12
15
101到200之間的質數
TestContinue.java
public class TestContinue {
  public static void main (String[] args) {
    for (int i = 101; i < 200; i +=2) {
      boolean f = true;
      for (int j = 2; j < i; j++) {
        if (i % j == 0) {
          f = false;
          break;
        }
      }
      if (!f) {continue;}
      System.out.println(" " + i);
    }
  }
}

C:\Users\root\Desktop>javac TestContinue.java

C:\Users\root\Desktop>java TestContinue
 101
 103
 107
 109
 113
 127
 131
 137
 139
 149
 151
 157
 163
 167
 173
 179
 181
 191
 193
 197
 199

switch() {
case XX:
...
case XX:
...
default:
...
}
當心case穿透,推薦使用break語句//不加break會穿透
多個case能夠合併到一塊兒
case 8: //等於8或者10都會打印B
case 10:
System.out.println("B");
break;
default能夠省略,但不推薦省略
java中switch語句只能探測int類型值//小括號裏面的值it

TestSwitch .java
public class TestSwitch {
  public static void main (String[] args) {
    int i = 18;
    switch (i) {
      case 8:
        System.out.println("A");
        break;
      case 10:
        System.out.println("B");
        break;
      case 18:
        System.out.println("C");
        break;
      default:
        System.out.println("error");
    }
  }
}
C:\Users\root\Desktop>javac TestSwitch.java

C:\Users\root\Desktop>java TestSwitch
C
相關文章
相關標籤/搜索