Java 1 3 java utility
This presentation is the property of its rightful owner.
Sponsored Links
1 / 77

Java 入門與進階設計 第 1 3 章: Java Utility PowerPoint PPT Presentation


  • 125 Views
  • Uploaded on
  • Presentation posted in: General

Java 入門與進階設計 第 1 3 章: Java Utility. 講師:紀俊男 [email protected] 本章重點. java.lang.Character java.lang.String java.lang.String Buffer java.lang.Number java.lang.Math. Character 物件. Java 提供了兩種處理字元的方法 char x = ‘ a ’ ; Character x = new Character( ‘ a ’ ); 特性比較. Character 物件.

Download Presentation

Java 入門與進階設計 第 1 3 章: Java Utility

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Java 1 3 java utility

Java 入門與進階設計第 13 章:Java Utility

講師:紀俊男

[email protected]


Java 1 3 java utility

本章重點

  • java.lang.Character

  • java.lang.String

  • java.lang.String Buffer

  • java.lang.Number

  • java.lang.Math


Character

Character 物件

  • Java 提供了兩種處理字元的方法

    • char x = ‘a’;

    • Character x = new Character(‘a’);

  • 特性比較


Character1

Character 物件

  • Character 建構元

    • Character(char value);

  • 型態轉換方法 (Casting Method)

    • 原始 Character 物件:Character c = new Character(‘a’);

    • 轉成字元:char x = c.charValue();

    • 轉成字串:String s = c.toString();


Character2

Character 物件

  • 測試函數:


Character3

Character 物件

  • 轉換函數


Character4

Character 物件

  • 範例

public class CharacterDemo {

public static void main(String args[]) {

Character a = new Character('a');

Character a2 = new Character('a');

Character b = new Character('b');

int difference = a.compareTo(b);

if (difference == 0) {

System.out.println("a is equal to b.");

} else if (difference < 0) {

System.out.println("a is less than b.");

} else if (difference > 0) {

System.out.println("a is greater than b.");

}


Character5

Character 物件

System.out.println("a is " + ((a.equals(a2)) ? "equal" : "not equal")

+ " to a2.");

System.out.println("The character " + a.toString() + " is "

+ (Character.isUpperCase(a.charValue()) ? "upper" : "lower")

+ "case.");

}

}

回到原主題


Java 1 3 java utility

為何有兩種字串物件

  • String 物件

    • 專門處理字串常數

    • 如:在 Java 程式中出現 “Hello! World!”,Java 即以 String 物件處理之。

    • 特性:一旦宣告後,其內容不可更動。

  • StringBuffer

    • 專門處理字串變數

    • 如:在 Java 中出現 “Hello!” + “World!”這類運算時,Java 會偷偷以 StringBuffer 處理。

    • 特性:字串可以做運算。


String

String 物件宣告

  • 只要是字串常數出現的地方,Java 就會偷偷宣告一個 String 物件:

    • String s = “Hello! World!”;

    • String s = new String(“Hello! World!”);

    • System.out.println(“Hello! World!”);

    • String s = new String(“Hello! World!”);int StrLen = s.length();與下面程式等效int StrLen = “Hello! World!”.length();


String1

String 物件簡介

  • 繼承物件

    • java.lang.Object  java.lang.String

  • 原始宣告

    • public final class String extends Object implements Serializable, Comparable


String2

String 物件常用方法

  • 字串長度

    • int length()

  • 範例:

    • String s = “Hello! World!”;System.out.println(s.length());

    • 結果:13


String3

String 物件常用方法

  • 字串比較

    • boolean equals(Object anObject);

    • boolean equalsIgnoreCase(String anString);

    • int compareTo(String anString);

    • int compareToIgnoreCase(String str);


String4

String 物件常用方法

  • 字串比較範例 (1)

public class StringCompare{

public static void main(String[] args){

String s1 = "Java";

String s2 = "Java";

String s3 = "java";

// Print out all string

System.out.println(s1 + "\n" + s2 + "\n" + s3 + "\n");

// "==" test

System.out.println((s1 == s2)? "s1 == s2":"s1 != s2");

// "==" test again

s2 = "Ja"; s2 = s2 + "va";

System.out.println((s1 == s2)? "s1 == s2":"s1 != s2");


String5

String 物件常用方法

  • 字串比較範例 (2)

// equals test

System.out.println((s1.equals(s2))? "s1 equals s2":"s1 not equals s2");

// equalsIgnoreCase test

System.out.println((s1.equals(s3))? "s1 equals s3":"s1 not equals s3");

System.out.println((s1.equalsIgnoreCase(s3))? "s1 equals s3 without case"

: "s1 not equals s3 without case");

//compareTo test

System.out.println("s1 compare to s3: " + s1.compareTo(s3));

System.out.println("s1 compare to s3 without case: " +

s1.compareToIgnoreCase(s3));

}

}


String6

String 物件常用方法

  • 字串連結

    • +

    • String concat (String str);

  • 範例

    • String s1 = “Hello!”;String s2 = “World!”;s1 += s2;

    • String s1 = “Hello!”;String s2 = “World!”;s1.concat(s2);


String7

String 物件常用方法

  • 去空白:

    • String trim();

  • 範例

    • String s = “ Hello! World! “;s.trim();System.out.println(s);

    • 結果:“Hello! World!”


String8

String 物件常用方法

  • 子字串操作

    • String substring(int beginIndex);

    • String substring(int beginIndex, int endIndex);

    • char charAt(int index);

    • int indexOf(int ch);

    • int indexOf(String str);

    • int lastIndexOf(int ch);

    • int lastIndexOf(String str);

    • boolean startsWith(String prefix);

    • boolean endsWith(String postfix);


String9

String 物件常用方法

  • 子字串操作範例

public class SubstringTest{

public static void main (String[] args){

String s = "This is a book";

System.out.println("String: " + s);

System.out.println("substring(8): " + s.substring(8));

System.out.println("substring(0, 6): " + s.substring(0, 6));

System.out.println("charAt(8): " + s.charAt(8));

System.out.println("indexOf('i'): " + s.indexOf('i'));

System.out.println("indexOf(\"is\"): " + s.indexOf("is"));

System.out.println("lastIndexOf('i'): " + s.lastIndexOf('i'));

System.out.println("lastIndexOf(\"is\"): " + s.lastIndexOf("is"));

System.out.println("startsWith(\"Th\"): " + s.startsWith("Th"));

System.out.println("endsWith(\"ok\"): " + s.endsWith("ok"));

}

}


String10

String 物件常用方法

  • 字串轉換

    • String toLowerCase();

    • String toUpperCase();

    • String valueOf(int i);

    • String valueOf(long l);

    • String valueOf(float f);

    • String valueOf(double d);

    • String valueOf(char c);

    • String valueOf(boolean b);


String11

String 物件常用方法

  • 字串轉換範例

public class StringConvert{

public static void main (String[] args){

String s = "This is a book";

System.out.println("String: " + s);

System.out.println("toLowerCase(): " + s.toLowerCase());

System.out.println("toUpperCase(): " + s.toUpperCase());

System.out.println("int 20 --> String: " + String.valueOf(20));

System.out.println("long 20L --> String: " + String.valueOf(20L));

System.out.println("float 20.0f --> String: " + String.valueOf(20.0f));

System.out.println("double 20.0d --> String: " + String.valueOf(20.0d));

System.out.println("char 'a' --> String: " + String.valueOf('a'));

System.out.println("boolean true --> String: " + String.valueOf(true));

}

}

回到原主題


Stringbuffer

StringBuffer 物件簡介

  • 繼承物件

    • java.lang.Object  java.lang.String

  • 原始宣告

    • Public final class StringBuffer extends Object implements Serializable

  • 特色

    • 可以修改字串內容,並加以操作


Stringbuffer1

StringBuffer 物件常用方法

  • 建構元

    • StringBuffer();

    • StringBuffer(int length);

    • StringBuffer(String str);


Stringbuffer2

StringBuffer 物件常用方法

  • 字串長度

    • int capacity();

    • int length();

    • void ensureCapacity(int minimumCapacity);

      • 將舊長度 * 2 + 2,直到大於等於要求長度為止

    • void setLength(int newLength)

      • 原字串較短者,不足部份填 0; 較長者,多餘部份裁掉。

  • 轉換函數

    • String toString();


Stringbuffer3

StringBuffer 物件常用方法

  • 字串長度範例

public class StringBufferLength{

public static void main (String[] args){

StringBuffer buf = new StringBuffer(20);

buf.append("This is a book");

System.out.println("StringBuffer: " + buf);

System.out.println("Capacity: " + buf.capacity());

System.out.println("Length: " + buf.length());

System.out.println("------------------------------");

buf.ensureCapacity(30);

System.out.println("Capacity: " + buf.capacity());

System.out.println("StringBuffer after ensureCapacity(30): " + buf);

buf.setLength(10);

System.out.println("Length: " + buf.length());

System.out.println("StringBuffer after setLength(10): " + buf);

}

}


Stringbuffer4

StringBuffer 物件常用方法

  • 字串操作

    • StringBuffer append(int i);

    • StringBuffer append(long l);

    • StringBuffer append(float f);

    • StringBuffer append(double d);

    • StringBuffer append(char c);

    • StringBuffer append(boolean b);

    • StringBuffer append(String str);


Stringbuffer5

StringBuffer 物件常用方法

  • 字串操作

    • StringBuffer insert(int offset, int i);

    • StringBuffer insert(int offset, long l);

    • StringBuffer insert(int offset, float f);

    • StringBuffer insert(int offset, double d);

    • StringBuffer insert(int offset, char c);

    • StringBuffer insert(int offset, boolean b);

    • StringBuffer insert(int offset, String str);


Stringbuffer6

StringBuffer 物件常用方法

  • 字串操作

    • StringBuffer replace(int start, int end, String str);

    • StringBuffer delete(int start, int end);

    • StringBuffer reverse();


Stringbuffer7

StringBuffer 物件常用方法

  • 字串操作範例

public class StringBufferOperation{

public static void main(String[] args){

StringBuffer buf = new StringBuffer();

buf.append("This is a book");

System.out.println("Append String: " + buf);

buf.insert(10, "bad ");

System.out.println("Insert String: " + buf);

buf.replace(10, 13, "good");

System.out.println("Replace String: " + buf);

buf.delete(10, 15);

System.out.println("Delete String: " + buf);

buf.reverse();

System.out.println("Reverse String: " + buf);

}

}


Stringbuffer8

StringBuffer 物件常用方法

  • 子字串操作

    • char charAt(int index);

    • void setCharAt(int index, char ch);

    • StringBuffer deleteCharAt(int index);

    • StringBuffer substring(int start);

    • StringBuffer substring(int start, int end);


Stringbuffer9

StringBuffer 物件常用方法

  • 子字串範例

public class SubstringBuffer{

public static void main(String[] args){

StringBuffer buf = new StringBuffer("This is a book");

System.out.println("String: " + buf);

System.out.println("charAt(10): " + buf.charAt(10));

buf.setCharAt(10, 'c');

System.out.println("setCharAt(10, 'c'): " + buf);

System.out.println("deleteCharAt(10): " + buf.deleteCharAt(10));

System.out.println("substring(10): " + buf.substring(10));

}

}


Java stringbuffer

Java 也偷偷用 StringBuffer

  • 當 Java 遇到如下的程式碼時:

    • String s = “This is”;System.out.println(s + “ a book”);

  • 事實上,它用如下的程式碼來處理:

    • String s = “This is”;System.out.println(new StringBuffer(s) . append(“ a book”).toString());


Java 1 3 java utility

總整理

  • String 與 StringBuffer 有何不同?

  • String 繼承何種物件?其原始宣告又是什麼?

  • 試述下列 String 物件常用方法的功能:

    • length()

    • equals() / equalsIgnoreCase()

    • compareTo() / compareToIgnoreCase()

    • concat() / trim()

    • substring() / charAt() / indexOf() / lastIndexOf()

    • startsWith() / endsWith()

    • toLowerCase() / toUpperCase()

    • valueOf()


Java 1 3 java utility

總整理

  • StringBuffer 繼承何種物件?其原始宣告又是什麼?

  • 試述下列 StringBuffer 物件常用方法的功能:

    • capacity() / length() / ensureCapacity() / setLength()

    • append() / insert() / replace() / delete() / reverse()

    • charAt() / setCharAt() / deleteCharAt()

    • substring()

回到原主題


Type wrapper

Type Wrapper 觀念介紹

  • 為什麼會有 Type Wrapper

int x = 3;

Integer x = new Integer(3);

x

0x5678

0x1234

x

3

0x5678

3

toString()

x = 3; 缺少操作此資料的函數 / 方法

x->Integer(3); 資料與方法同在,方便


Type wrapper1

Type Wrapper 觀念介紹

  • Type Wrapper 有哪些


Type wrapper2

Type Wrapper 觀念介紹

  • Number (java.lang)

    • 所有 Type Wrapper 之父類別

  • Byte, Short, Integer, Long (java.lang)

    • 對應 byte, short, int, long 等 Primitive Type

  • Float, Double (java.lang)

    • 對應 float, double 等 Primitive Type

  • BigInteger, BigDecimal (java.math)

    • 允許程式師儲存 Primitive Type 無法表示的數值長度及精確度


Type wrapper3

Type Wrapper 觀念介紹

  • 為了完整對應所有的 Primitive Type,下列三者 Type Wrapper 被創造出來,但不繼承 java.lang.Number

    • Boolean (java.lang) boolean

    • Character (java.lang) char

    • Void (java.lang)  void


Number

Number常用屬性及方法

  • Number 類別

    • 繼承 java.lang.Object

    • abstract class  意即:僅供繼承,不供產生實體


Number1

Number常用屬性及方法

  • Number 常用屬性


Number2

Number常用屬性及方法

  • Number 常用方法

    • byte byteValue()

    • short shortValue()

    • abstract int intValue()

    • abstract long longValue()

    • abstract float floatValue()

    • abstract double doubleValue()


Number3

Number 範例

public class NumberDemo{

public static void main(String[] args){

Number n = new Number();

}

}

Error Message:

NumberDemo.java:3: java.lang.Number is abstract; cannot be instantiated

Number n = new Number();

^

1 error


Byte short

Byte / Short 常用屬性及方法

  • Byte 類別

    • 繼承 java.lang.Number

    • final class 意即:無法再被其它類別繼承

    • public final class Byte extends Number implements Comparable

  • Short 類別

    • 繼承 java.lang.Number

    • final class 意即:無法再被其它類別繼承

    • public final class Short extends Number implements Comparable


Byte short1

Byte / Short 常用屬性及方法

  • 建構元

    • Byte(byte value);Short(short value);

    • Byte(String s);Short(String s);

  • 範例

    • Byte b = new Byte(100);

    • Short s = new Short(“1234”);


Byte short2

Byte / Short 常用屬性及方法

  • Byte / Short 常用屬性

    • .MIN_VALUE

      • Byte.MIN_VALUE  傳回 –128

      • Short.MIN_VALUE  傳回 –32768

    • .MAX_VALUE

      • Byte.MAX_VALUE  傳回 +127

      • Short.MAX_VALUE  傳回 +32767


Byte short3

Byte / Short 常用屬性及方法

  • Byte / Short 常用方法(繼承自 java.lang.Number)

    • byte byteValue();

    • short shortValue();

    • int intValue();

    • long longValue();

    • float floatValue();

    • double doubleValue();


Byte short4

Byte / Short 常用屬性及方法

  • Byte / Short 常用方法(實作 java.lang.Comparable 介面)

    • int compareTo(Byte obj);int compareTo(Short obj);

    • boolean equals(Object obj);

  • 範例

    • Byte b1 = new Byte(3);Byte b2 = new Byte(4);b1.compareTo(b2);b1.equals(b2);


Byte short5

Byte / Short 常用屬性及方法

  • Byte / Short 常用方法(字串轉數字)

    • static Byte valueOf(String s);

    • static Short valueOf(String s);

      • 傳入十進位字串,傳回數值物件

    • static Byte decode(String s);

    • static Short decode(String s);

      • 傳入十、十六、八進位字串,傳回數值物件

    • static byte parseByte(String s);

    • static short parseShort(String s);

      • 傳入十進位字串,傳回數值本身


Byte short6

Byte / Short 常用屬性及方法

  • Byte / Short 常用方法(數字轉字串)

    • String toString()


Byte short7

Byte / Short 範例

public class ByteShortDemo{

public static void main(String[] args){

System.out.println("MAX of byte: " + Byte.MAX_VALUE);

System.out.println("MIN of short: " + Short.MIN_VALUE);

Byte b = Byte.valueOf("100");

System.out.println("Value of 100 with Byte: " + b.toString());

Short s = Short.decode("0x100");

System.out.println("Value of 0x100 with Short: " + s.toString());

System.out.println("Value of 2000 with Short: " + Short.parseShort("2000"));

}

}


Integer long

Integer / Long 常用屬性及方法

  • Integer 類別

    • 原型宣告public final class Integer extends Number implements Comparable

  • Long 類別

    • 原型宣告public final class Long extends Number implements Comparable


Integer long1

Integer / Long 常用屬性及方法

  • 建構元

    • Integer(int value);Long(long value);

    • Integer(String s);Long(String s);

  • 範例

    • Integer iObj = new Integer(12345);

    • Long lObj = new Long(“45678”);


Integer long2

Integer / Long 常用屬性及方法

  • 常用屬性

    • static int MAX_VALUE;static long MAX_VALUE;

    • static int MIN_VALUE;static long MIN_VALUE;


Integer long3

Integer / Long 常用屬性及方法

  • 常用方法(繼承自 java.lang.Number)

    • byte byteValue();

    • short shortValue();

    • int intValue();

    • long longValue();

    • float floatValue();

    • double doubleValue();


Integer long4

Integer / Long 常用屬性及方法

  • 常用方法(實作 java.lang.Comparable)

    • int compareTo(Integer obj);int compareTo(Long obj);

    • boolean equals(Object obj);

  • 範例

    • Integer i1 = new Integer(3);Integer i2 = new Integer(4);i1.compareTo(i2);i1.equals(i2);


Integer long5

Integer / Long 常用屬性及方法

  • 常用方法(字串轉數字)

    • static Integer valueOf(String s);

    • static Long valueOf(String s);

      • 傳入十進位字串,傳回數值物件

    • static Integer decode(String s);

    • static Long decode(String s);

      • 傳入十、十六、八進位字串,傳回數值物件

    • static int parseByte(String s);

    • static long parseShort(String s);

      • 傳入十進位字串,傳回數值本身


Integer long6

Integer / Long常用屬性及方法

  • 常用方法(數字轉字串)

    • String toString();

    • static String toBinaryString(int i);static String toBinaryString(long l);

    • static String toOctalString(int i);static String toOctalString(long l);

    • static String toHexString(int i);static String toHexString(long l);


Integer long7

Integer / Long 範例

public class IntLongDemo{

public static void main(String[] args){

Integer i = new Integer(100);

System.out.println("Decimal of 100: " + i.toString());

System.out.println("Hexidecimal of 100: " + Integer.toHexString(i.intValue()));

System.out.println("Octal of 100: " + Integer.toOctalString(i.intValue()));

System.out.println("Binary of 100: " + Integer.toBinaryString(i.intValue()));

}

}


Float double

Float / Double 常用屬性及方法

  • Float 類別

    • public final class Float extends Number implements Comparable

  • Double 類別

    • public final class Double extends Number implements Comparable


Float double1

Float / Double 常用屬性及方法

  • 建構元

    • Float(float f);

    • Float(double f);Double(double d);

    • Float(String s);Double(String d);

  • 範例

    • Float f = new Float(3.0f);

    • Double d = new Double(“4.0”);


Float double2

Float / Double 常用屬性及方法

  • 常用屬性

    • MAX_VALUE

      • 傳回兩者的極大值

    • MIN_VALUE

      • 傳回兩者的極小值

    • POSITIVE_INFINITY

      • 正無限大。1.0 / 0.0 會得到此值

    • NEGATIVE_INFINITY

      • 負無限大。-1.0 / 0.0 會得此值

    • NaN (Not a Number)

      • 非數字。可能為 0.0 / 0.0 的結果或虛數 ( ) 的運算結果


Float double3

Float / Double 常用屬性及方法

  • 補充說明

    • POSITIVE_INFINITY by IEEE 754

      • 單精確度:0x7f800000

      • 雙精確度:0x7ff0000000000000L

    • NEGATIVE_INFINITY by IEEE 754

      • 單精確度:0xff800000

      • 雙精確度:0xfff0000000000000L

    • NaN by IEEE 754

      • 單精確度:0x7fc00000

      • 雙精確度:0x7ff8000000000000L


Float double4

Float / Double 常用屬性及方法

  • 常用方法(繼承自 java.lang.Number)

    • byte byteValue();

    • short shortValue();

    • int intValue();

    • long longValue();

    • float floatValue();

    • double doubleValue();


Float double5

Float / Double 常用屬性及方法

  • 常用方法(實作 java.lang.Comparable 介面)

    • static int compare(float f1, float f2);static int compare(double d1, double d2);

    • int compareTo(Float obj);int compareTo(Double obj);

    • boolean equals(Object obj);


Float double6

Float / Double 常用屬性及方法

  • 常用方法(字串轉數字)

    • static Float valueOf(String s);

    • static Double valueOf(String s);

      • 傳入字串,傳回數值物件

    • static float parseByte(String s);

    • static double parseShort(String s);

      • 傳入字串,傳回數值本身


Float double7

Float / Double 常用屬性及方法

  • Byte / Short 常用方法(數字轉字串)

    • String toString()


Float double8

Float / Double 常用屬性及方法

  • 常用方法

    • static int floatToIntBits(float value);

    • static long doubleToLongBits(double value);

      • 傳入數值,傳出排成 IEEE 754 格式的 bit pattern

    • static float intBitsToFloat(int bits);

    • static double longBitsToDouble(long bits);

      • 傳入 IEEE 754 格式,傳出代表該格式的浮點數


Float double9

Float / Double 常用屬性及方法

  • 常用方法

    • boolean isInfinite();

      • 如果是 POSITIVE_INFINITY 或 NEGATIVE_INFINITY 則傳回 true

    • boolean isNaN();

      • 如果是 NaN,則傳回 true


Float double10

Float / Double 範例

public class FloatTest{

public static void main(String[] args){

Float f1 = new Float(1.0/0.0);

Float f2 = new Float(-1.0/0.0);

Float f3 = new Float(0.0/0.0);

System.out.println("The result of 1.0 / 0.0: " + f1.toString());

System.out.println("The result of -1.0 / 0.0: " + f2.toString());

System.out.println("The result of 0.0 / 0.0: " + f3.toString());

}

}

回到原主題


Java 1 3 java utility

Math 物件

  • 用途

    • 提供各類數學函數的類別。

  • 常用屬性

    • Math.E: 傳回自然數 e = 2.718281828…

    • Math.PI: 傳回圓周率 pi = 3.141592653…


Java 1 3 java utility

Math 物件

  • 常用方法:max 與 min

    • Math.max()

      • int max(int, int)

      • long max(long, long)

      • float max(float, float)

      • double max(double, double)

    • Math.min()

      • int min(int, int)

      • long min(long, long)

      • float min(float, float)

      • double min(double, double)


Java 1 3 java utility

Math 物件

  • 常用方法:亂數與小數之捨入

    • Math.random(): 傳回 0.0 ~ 1.0 之間的亂數

    • Math.round(double): 四捨五入後傳回整數

    • Math.ceil(double): 無條件進入

    • Math.floor(double): 無條件捨去

    • Math.rint(double): 傳回最接近傳入值的整數 (常用於負數的小數捨入)

    • Math.abs(n): 傳回絕對值


Java 1 3 java utility

Math 物件

  • 常用方法:三角函數

    • Math.sin(double)

    • Math.cos(double)

    • Math.tan(double)

    • Math.asin(double)

    • Math.acos(double)

    • Math.atan(double)

    • Math.toDegrees(double): 轉換為角度

    • Math.toRadians(double): 轉換為弳度


Java 1 3 java utility

Math 物件

  • 常用方法:對數與指數

    • Math.exp(double): 自然指數 ex

    • Math.log(double): 自然對數 ln(x)

    • Math.sqrt(double): 開根號

    • Math.pow(double, double): 次方 Xy


Java 1 3 java utility

Math 物件範例

public class MathTest{

public static void main(String[] args) {

// 顯示數學常數

System.out.println("E: " + Math.E);

System.out.println("PI: " + Math.PI);

// 數學函數

double no = -19.536;

System.out.println("測試值no: " + no);

System.out.println("abs(no): " + Math.abs(no));

System.out.println("ceil(no): " + Math.ceil(no));

System.out.println("floor(no): " + Math.floor(no));

System.out.println("rint(no): " + Math.rint(no));


Java 1 3 java utility

Math 物件範例

// 指數和對數函數

double x = 13.536;

double y = 3.57;

System.out.println("測試值x/y: " + x + "/" +y);

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

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

System.out.println("pow(x,y): " + Math.pow(x,y));

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

// 三角函數

double deg = 60.0;

double rad = Math.toDegrees(deg);

System.out.println("測試值deg/rad: " + deg + "/" + rad);

System.out.println("sin(rad): " + Math.sin(rad));

System.out.println("cos(rad): " + Math.cos(rad));

System.out.println("tan(rad): " + Math.tan(rad));

}

}


Java 1 3 java utility

總整理

  • Math 提供哪些常數?

  • 試述 round() 與 rint() 之不同

  • Math 的三角函數使用角度還是弳度?

  • 如何使用 Math 來轉換角度與弳度


  • Login