Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.

Similar presentations


Presentation on theme: "1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008."— Presentation transcript:

1

2 1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008

3 Basic Mathematical Operators Each of the operators in the table are binary operators. A binary operator acts on two operands Java Operation Arithmetic Operator Algebraic Expression Java Expression 加法 +a + b 減法 -a – b 乘法 *aba * b 除法 /a / b 餘數 %a mod ba % b

4 3 進行各種運算 ─ 使用變數做為運算元 class Sample2 { public static void main(String args[]) { int num1 = 2; int num2 = 3; int sum = num1+num2; System.out.println(" 變數 num1 的值是 " + num1 + " 。 "); System.out.println(" 變數 num2 的值是 " + num2 + " 。 "); System.out.println("num1+num2 的值是 " + sum + " 。 "); num1 = num1+1; System.out.println(" 變數 num1 的值加 1 後是 " + num1 + " 。 "); }

5 Example: Integer Division // Integer and Modulus Division public class DivMod { public static void main( String args[] ) { int x = 5, y = 10; System.out.println ("5 / 10: " + x/y); System.out.println ("5 % 10: " + x%y); } 5 / 10: 0 5 % 10: 5 //Common Programming Error: Dividing by zero is normally undefined //on computer systems and generally results in a fatal error.

6 5 運算子的執行優先順序 (Operator Precedence) 當在 Java 程式中同時出現了二種不同的運算子,您必須區 分式子當中運算子的執行優先順序,才能產生正確的執行 結果。 當運算子的優先順序相同時 同一行 Java 程式碼當中,如果所有的運算子優先順序相 同時,應該遵循「由左至右」的原則。 改變運算子的執行順序 程式碼當中的「括號」,其實就是改變運算子優先順序 的方法,用括號先框起來才會優先計算。 What is the answer of this x = 5 + 6 * 7

7 6 常數 (Constants) public class circle_area_pi { public static void main(String[] args) { final double PI = 3.14159; double radius, area; // declare variables radius = 3.00; // assign radius of the circle area = radius * radius * PI ; System.out.println("The area of the circle of radius " + radius + " is " + area); } // 依 ” 慣例 ” ,常數名稱由大寫字母組成

8 Floating point Data Type // Double Example Program public class Double { public static void main( String args[] ) { double var1, var2, var3, sum; var1 = 87.25; var2 = 92.50; var3 = 96.75; sum = var1 + var2 + var3; System.out.println ("Sum: " + sum); } Sum: 276.5

9 8 Numeric type ranges in Java Integers Floating point values

10 Example: Find an Average 計算平均分數. int totalTests = 4; double average = 90+92+95+100/totalTests; 問題 #1: Operator Precedence : 應該使用括號 ( ) double average = (90+92+95+100)/totalTests; 問題 #2: 90, 92, 95, 100 跟 4 都是整數. 因此,等號右邊計算的結果是整數 90. 所以變數 average 會被設成 94.0, 而不是 94.25.

11 資料型態的轉換 當一個運算中有 ints 也有 doubles 時,所有的運算元 (operands) 會被當成 double 來運算 在上個例子中我們可以利用下列的方式來得到正確答案 94.25: 1. 宣告 totalTests 為 double: double totalTests = 4.0; double average = (90+92+95+100)/totalTests; 2. 直接除以 4.0 double average = (90+92+95+100) / 4.0; 3. 使用 Cast Operator int totalTests = 4; double average = (90+92+95+100)/(double)totalTests;

12 11 more on casting 指定變數值時,可以把 “ 資料型態小的變數值 ” 指定給 “ 資料型態大的變數 ” ,例如 – 把 byte 的數值指定給 int – 把 int 的數值指定給 double – 把 float 的數值指定給 double 必須用 cast 的方式才可以把 “ 資料型態大的變數值 ” 指 定給 “ 資料型態小的變數 ” ,例如 : –byte gets (byte) int –int gets (int) double –float gets (float) double ( 不用 cast 會有 syntax error)

13 Warning about floating point values 浮點數 (Floating point values) 有時不是很準確,顯示時 也可能跟你想的不同 –1.9 可能顯示成 1.89999999999999999999999 –1.9 可能不等於 1.9! –0.1 加 10 次不見得等於 1

14 13 Other Arithmetic Operators ( 其他的運算子 ) public class OtherOperators { public static void main(String[] args) { int x = 10; // declare variables int y = 5; int z = 3; System.out.println("x = "+ x + ", y = “ + y + ", z = “ + z + "\n"); x++; y += x; z *= x; System.out.println("Now x = "+ x +", y = "+ y + ", z = "+ z + "\n");

15 14 Other Arithmetic Operators x--; y *= x; z %= x; System.out.println("And now x = " + x + ", y = " + y + ", z = " + z + "\n"); System.exit(0); }

16 15 運算子的種類 主要的運算子種類有下列幾種: + ( 加號 )| ( OR )< ( 小於 ) - ( 減法 )^ ( XOR )<= ( 小於等於 ) * ( 乘法 )<< ( 左移 )== ( 相等 ) / ( 除法 )>> ( 右移 )!= ( 不等於 ) % ( 餘數 )>>> ( 右移補零 )! ( 邏輯 NOT ) + ( 正號 )++ ( 遞增 )&& ( 邏輯 AND ) - ( 負號 )-- ( 遞減 )|| ( 邏輯 OR ) ~ ( 補數 )> ( 大於 )?: ( 條件式 ) & ( AND )>= ( 大於等於 )

17 16 指定運算子 指定運算子主要是用來將資料指定給變數。總之,指 定運算子的功用是:將右邊的值(不管文字或數字), 指定給左邊的變數,指定運算子有下列幾種: += 加法後指定數值給變數 ^= 邏輯 XOR 後指定數值給變數 -= 減法後指定數值給變數 |= 邏輯 OR 後指定數值給變數 *= 乘法後指定數值給變數 <<= 位元左移後指定數值給變數 /= 除法後指定數值給變數 >>= 位元右移後指定數值給變數 %= 求餘後指定數值給變數 >>>= 位元右移並補零再指定數 值給變數 &= 邏輯 AND 後指定數值給變數


Download ppt "1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008."

Similar presentations


Ads by Google