超越基本算術(shù)
Java編程語(yǔ)言支持基本算術(shù)及其算術(shù)運(yùn)算符:+、-、*、/和%,java.lang包中的Math類提供了用于執(zhí)行更高級(jí)數(shù)學(xué)計(jì)算的方法和常量。
Math類中的方法都是靜態(tài)的,因此你可以直接從類中調(diào)用它們,如下所示:
Math.cos(angle);
使用靜態(tài)導(dǎo)入語(yǔ)言功能,你不必在每個(gè)數(shù)學(xué)函數(shù)前面寫Math:
import static java.lang.Math.*;
這允許你通過(guò)簡(jiǎn)單名稱調(diào)用Math類方法,例如:
cos(angle);常量和基本方法
Math類包含兩個(gè)常量:
Math.E,是自然對(duì)數(shù)的基數(shù)。
Math.PI,這是圓周率。
Math類還包含40多種靜態(tài)方法,下表列出了許多基本方法。
方法 | 描述 |
---|---|
double abs(double d) float abs(float f) int abs(int i) long abs(long lng) |
返回參數(shù)的絕對(duì)值。 |
double ceil(double d) | 返回大于或等于參數(shù)的最小整數(shù),作為double返回。 |
double floor(double d) | 返回小于或等于參數(shù)的最大整數(shù),作為double返回。 |
double rint(double d) | 返回與參數(shù)值最接近的整數(shù),作為double返回。 |
long round(double d) int round(float f) |
根據(jù)方法的返回類型,返回與參數(shù)最近的long或int。 |
double min(double arg1, double arg2) float min(float arg1, float arg2) int min(int arg1, int arg2) long min(long arg1, long arg2) |
返回兩個(gè)參數(shù)中較小的一個(gè)。 |
double max(double arg1, double arg2) float max(float arg1, float arg2) int max(int arg1, int arg2) long max(long arg1, long arg2) |
返回兩個(gè)參數(shù)中較大的一個(gè)。 |
以下程序BasicMathDemo說(shuō)明了如何使用其中一些方法:
public class BasicMathDemo { public static void main(String[] args) { double a = -191.635; double b = 43.74; int c = 16, d = 45; System.out.printf("The absolute value " + "of %.3f is %.3f%n", a, Math.abs(a)); System.out.printf("The ceiling of " + "%.2f is %.0f%n", b, Math.ceil(b)); System.out.printf("The floor of " + "%.2f is %.0f%n", b, Math.floor(b)); System.out.printf("The rint of %.2f " + "is %.0f%n", b, Math.rint(b)); System.out.printf("The max of %d and " + "%d is %d%n", c, d, Math.max(c, d)); System.out.printf("The min of of %d " + "and %d is %d%n", c, d, Math.min(c, d)); } }
這是該程序的輸出:
The absolute value of -191.635 is 191.635 The ceiling of 43.74 is 44 The floor of 43.74 is 43 The rint of 43.74 is 44 The max of 16 and 45 is 45 The min of 16 and 45 is 16指數(shù)和對(duì)數(shù)方法
下表列出了Math類的指數(shù)和對(duì)數(shù)方法。
方法 | 描述 |
---|---|
double exp(double d) | 返回自然對(duì)數(shù)的基數(shù)e的參數(shù)次方。 |
double log(double d) | 返回參數(shù)的自然對(duì)數(shù)。 |
double pow(double base, double exponent) | 返回第一個(gè)參數(shù)的值提升到第二個(gè)參數(shù)的次冪。 |
double sqrt(double d) | 返回參數(shù)的平方根。 |
以下程序ExponentialDemo顯示e的值,然后對(duì)任意選擇的數(shù)字調(diào)用上表中列出的每個(gè)方法:
public class ExponentialDemo { public static void main(String[] args) { double x = 11.635; double y = 2.76; System.out.printf("The value of " + "e is %.4f%n", Math.E); System.out.printf("exp(%.3f) " + "is %.3f%n", x, Math.exp(x)); System.out.printf("log(%.3f) is " + "%.3f%n", x, Math.log(x)); System.out.printf("pow(%.3f, %.3f) " + "is %.3f%n", x, y, Math.pow(x, y)); System.out.printf("sqrt(%.3f) is " + "%.3f%n", x, Math.sqrt(x)); } }
這是運(yùn)行ExponentialDemo時(shí)你將看到的輸出:
The value of e is 2.7183 exp(11.635) is 112983.831 log(11.635) is 2.454 pow(11.635, 2.760) is 874.008 sqrt(11.635) is 3.411三角函數(shù)的方法
Math類還提供了三角函數(shù)功能的集合,如下表所示,傳遞給每個(gè)方法的值是以弧度表示的角度,你可以使用toRadians方法將度數(shù)轉(zhuǎn)換為弧度。
方法 | 描述 |
---|---|
double sin(double d) | 返回指定double值的正弦值。 |
double cos(double d) | 返回指定double值的余弦值。 |
double tan(double d) | 返回指定double值的正切值。 |
double asin(double d) | 返回指定double值的反正弦值。 |
double acos(double d) | 返回指定double值的反余弦值。 |
double atan(double d) | 返回指定double值的反正切值。 |
double atan2(double y, double x) | 將直角坐標(biāo)(x,y)轉(zhuǎn)換為極坐標(biāo)(r,theta)并返回theta。 |
double toDegrees(double d) double toDegrees(double d) |
將參數(shù)轉(zhuǎn)換為度數(shù)或弧度。 |
這是一個(gè)程序TrigonometricDemo,它使用這些方法中的每一個(gè)來(lái)計(jì)算45度角的各種三角函數(shù)值:
public class TrigonometricDemo { public static void main(String[] args) { double degrees = 45.0; double radians = Math.toRadians(degrees); System.out.format("The value of pi " + "is %.4f%n", Math.PI); System.out.format("The sine of %.1f " + "degrees is %.4f%n", degrees, Math.sin(radians)); System.out.format("The cosine of %.1f " + "degrees is %.4f%n", degrees, Math.cos(radians)); System.out.format("The tangent of %.1f " + "degrees is %.4f%n", degrees, Math.tan(radians)); System.out.format("The arcsine of %.4f " + "is %.4f degrees %n", Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians)))); System.out.format("The arccosine of %.4f " + "is %.4f degrees %n", Math.cos(radians), Math.toDegrees(Math.acos(Math.cos(radians)))); System.out.format("The arctangent of %.4f " + "is %.4f degrees %n", Math.tan(radians), Math.toDegrees(Math.atan(Math.tan(radians)))); } }
該程序的輸出如下:
The value of pi is 3.1416 The sine of 45.0 degrees is 0.7071 The cosine of 45.0 degrees is 0.7071 The tangent of 45.0 degrees is 1.0000 The arcsine of 0.7071 is 45.0000 degrees The arccosine of 0.7071 is 45.0000 degrees The arctangent of 1.0000 is 45.0000 degrees隨機(jī)數(shù)
random()方法返回一個(gè)介于0.0和1.0之間的偽隨機(jī)選擇的數(shù)字,范圍包括0.0但不包括1.0,換句話說(shuō):0.0 <= Math.random() < 1.0。要獲取不同范圍內(nèi)的數(shù)字,可以對(duì)隨機(jī)方法返回的值執(zhí)行算術(shù)運(yùn)算,例如,要生成0到9之間的整數(shù),你可以編寫:
int number = (int)(Math.random() * 10);
通過(guò)將該值乘以10,可能值的范圍變?yōu)?b>0.0 <= number < 10.0。
當(dāng)你需要生成單個(gè)隨機(jī)數(shù)時(shí),使用Math.random可以很好地工作,如果需要生成一系列隨機(jī)數(shù),則應(yīng)創(chuàng)建java.util.Random實(shí)例并在該對(duì)象上調(diào)用方法以生成數(shù)字。
上一篇:格式化數(shù)字打印輸出 下一篇:字符文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/72888.html
字符 大多數(shù)情況下,如果使用單個(gè)字符值,則將使用原始char類型,例如: char ch = a; // Unicode for uppercase Greek omega character char uniChar = u03A9; // an array of chars char[] charArray = { a, b, c, d, e }; 但是,有時(shí)候需要使用字符作為對(duì)象 — 例如...
Java? 教程 Java教程是為JDK 8編寫的,本頁(yè)面中描述的示例和實(shí)踐沒(méi)有利用在后續(xù)版本中引入的改進(jìn)。 Java教程是希望使用Java編程語(yǔ)言創(chuàng)建應(yīng)用程序的程序員的實(shí)用指南,其中包括數(shù)百個(gè)完整的工作示例和數(shù)十個(gè)課程,相關(guān)課程組被組織成教程。 覆蓋基礎(chǔ)知識(shí)的路徑 這些教程以書(shū)籍的形式提供,如Java教程,第六版,前往Amazon.com購(gòu)買。 入門 介紹Java技術(shù)和安裝Java開(kāi)發(fā)軟件并使用...
格式化數(shù)字打印輸出 之前你已經(jīng)看到使用print和println方法將字符串打印到標(biāo)準(zhǔn)輸出(System.out),由于所有數(shù)字都可以轉(zhuǎn)換為字符串(你將在本課后面看到),你可以使用這些方法打印出任意的字符串和數(shù)字混合,但是,Java編程語(yǔ)言還有其他方法,可以在包含數(shù)字時(shí)對(duì)打印輸出進(jìn)行更多控制。 printf和format方法 java.io包中包含一個(gè)PrintStream類,它有兩種格式化方法可...
摘要:示例字符串?dāng)?shù)值算術(shù)和文件原文譯者飛龍協(xié)議大量的教程和文章都涉及到中最重要的改變,例如表達(dá)式和函數(shù)式數(shù)據(jù)流。不僅僅是字符串,正則表達(dá)式模式串也能受益于數(shù)據(jù)流。 Java 8 API 示例:字符串、數(shù)值、算術(shù)和文件 原文:Java 8 API by Example: Strings, Numbers, Math and Files 譯者:飛龍 協(xié)議:CC BY-NC-SA 4.0 ...
運(yùn)算符 既然你已經(jīng)學(xué)會(huì)了如何聲明和初始化變量,那么你可能想知道如何使用它們,學(xué)習(xí)Java編程語(yǔ)言的運(yùn)算符是一個(gè)很好的起點(diǎn),運(yùn)算符是對(duì)一個(gè)、兩個(gè)或三個(gè)操作數(shù)執(zhí)行特定運(yùn)算的特殊符號(hào),然后返回結(jié)果。 在我們探索Java編程語(yǔ)言的運(yùn)算符時(shí),提前知道哪些運(yùn)算符具有最高優(yōu)先級(jí)可能會(huì)對(duì)你有所幫助,下表中的運(yùn)算符按優(yōu)先順序列出,運(yùn)算符出現(xiàn)在離表頂部越近,其優(yōu)先級(jí)越高,優(yōu)先級(jí)較高的運(yùn)算符在優(yōu)先級(jí)相對(duì)較低的運(yùn)算符之前...
閱讀 1223·2019-08-30 12:44
閱讀 710·2019-08-29 13:03
閱讀 2642·2019-08-28 18:15
閱讀 2484·2019-08-26 10:41
閱讀 3167·2019-08-26 10:28
閱讀 3092·2019-08-23 16:54
閱讀 2065·2019-08-23 15:16
閱讀 893·2019-08-23 14:55