package a.b; public class Three { static void Expression() { System.out.println("1、學習基本的表達式"); // 數學表達式 System.out.println( 1 + 2 ); //加法 System.out.println( 4 - 3.2 ); //減法 System.out.println( 7 * 1.5); //乘法 System.out.println( 29/7 ); //除法 System.out.println( 29%7 ); //求餘 // 關係表達式 System.out.println(6 > 4.2); //大於 System.out.println(3.4 >= 3.4); //大於等於 System.out.println(1.5 < 9 ); //小於 System.out.println( 6 <= 1); // 小於等於 System.out.println(2 == 2); //等於 System.out.println(2 != 2 ); //不等於 /* 其餘的一些表達式 true && false and (3 > 1) || (2 == 1) or !true not // 位運算 & and | or ^ xor ~ not 5 << 3 0b101 left shift 3 bits 6 >> 1 0b110 right shift 1 bit m++ 變量m加1 n-- 變量n減1 condition ? x1 : x2 condition爲一個boolean值。根據condition,取x1或x2的值 */ } static void Controlstructure() { System.out.println("2、學習控制結構"); int a = 0; // 選擇語句 if (a == 1) { System.out.println("學習if語句 if "); } else if (a == 0) { System.out.println("學習if語句 else if "); } else { System.out.println("學習if語句 else "); } // 循環語句 while(a<3) { System.out.println("學習while語句"+a); a++; } do { System.out.println("學習do while語句"+a); a++; }while(a<6); // 使用break能夠跳出循環、使用continue能夠直接進入下一循環 for (int i = 0; i < 5; i++) { if (i == 1) { continue; } if (i == 4 ) { break; } System.out.println("學習for語句"+i); } // 選擇語句 char c = 'b'; switch (c) { case 'a':System.out.println("學習swithch語句"+c); break; case 'b':System.out.println("學習swithch語句"+c); break; case 'c':System.out.println("學習swithch語句"+c); break; default:System.out.println("學習swithch語句 default"); break; } } public static void main(String[] args) { // 學習基本的表達式 Expression(); // 學習控制結構 Controlstructure(); } }
運行結果以下:java
1、學習基本的表達式
3
0.7999999999999998
10.5
4
1
true
true
true
false
true
false
2、學習控制結構
學習
if
語句
else
if
學習
while
語句
0
學習
while
語句
1
學習
while
語句
2
學習
do
while
語句
3
學習
do
while
語句
4
學習
do
while
語句
5
學習
for
語句
0
學習
for
語句
2
學習
for
語句
3
學習swithch語句b
|