首頁 > Java > java教程 > 主體

Java 最大()

王林
發布: 2024-08-30 15:38:37
原創
503 人瀏覽過

java max() 函數用來傳回兩個數值中的最大值。 java max()函數是java中的內建函數,它定義在Java.lang.math類別中,因此要在程式中使用max()函數,必須導入Java.lang.math類別。 max() 函數接受兩個數值資料型別的參數,並傳回兩個數值參數中的最大值;它可以針對 int、float、double 和 long 等資料型別的不同參數進行重載。它不會拋出任何異常。

廣告 該類別中的熱門課程 3DS MAX 架構 - 專業化 | 4 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

Java max() 語法

以下是文法:

文法:

data_Type max(data_Type x, data_Type y)
登入後複製

資料型別可以是 int、float、double 和 long。

以下是 max() 函數的參數:

x和y是兩個數值參數,要傳回其中最大的數字。

傳回值:函數的回傳值最多為兩個數字,與傳入參數的data_type相同。

java中的max()函數可以針對不同資料類型的參數進行重載,因此以下是java中max()函數的語法:

public static int max(int x, int y);
登入後複製
public static float max(float x, float y) ;
登入後複製
public static long max( long x, long y );
登入後複製
public static double max(double x, double y);
登入後複製
注意:如果參數以正數和負數傳遞,則函數會傳回正結果。如果兩個參數都以負數傳遞,則函數會傳回較低量值的結果。如果參數作為相同的值傳遞,則函數會傳回相同的結果。如果任一參數以 NAN 值傳遞,則函數將傳回 NAN 作為結果。

實作 Java max() 函數的範例

以下是範例:

範例#1

我們寫java程式碼是為了更清楚地理解max()函數,下面的範例中我們使用max()函數來找出使用者傳遞的兩個整數中的最大值,如下所示。

代碼:

import java.lang.Math;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x = 1024, y = 2034, result;
// calling max() built in function which is define in Math built in class
result = Math.max(x,y);
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
登入後複製

輸出:

Java 最大()

說明:

如上面的程式碼,x 和y 是兩個變量,分別宣告並初始化為1024 和2034 個值,稍後,將這些變數作為參數呼叫max() 函數(int max(int x, int y )重載)因此函數的結果是2034,這是1024 和2034 中的最大數字。

範例#2

我們寫java程式碼來理解max()函數,其中我們使用max()函數來找出使用者傳遞的兩個雙精度數中的最大值,如下所示。

代碼:

import java.lang.Math;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
double x = 134.78, y = 56.89, result;
// calling max() built in function which is define in Math built in class
result = Math.max(x,y);
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
登入後複製

輸出:

Java 最大()

說明:

如上面的程式碼,x和y是兩個變量,分別宣告並初始化為134.78和56.89 double值,隨後,呼叫max()函數並將這些變數作為參數傳遞(double max(double x, double y ) 重載) 因此函數的結果是134.78,這是1024 和2034 中的最大數字。

範例#3

我們寫java程式碼來理解max()函數,其中我們使用max()函數來找出使用者從控制台接受的兩個整數中的最大值。

代碼:

import java.lang.Math;
import java.util.Scanner;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x, y, result;
// accept two numbers of integer type from console
System.out.println( "Enter the Two Numeric value: ");
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
y = sc.nextInt();
result = Math.max(x,y);
//Print the maximum number between x and y
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
登入後複製

輸出:

Java 最大()

如上面的程式碼所示,x 和 y 是兩個變量,它們透過掃描器物件宣告並接受使用者的這些變數值。隨後,呼叫 max() 函數並將這些變數作為參數傳遞,因此函數基於使用者輸入的值(例如 10 和 20)的結果是 20。

範例#4

我們為 max() 函數編寫 java 程式碼,其中使用 max() 函數傳遞兩個負值並找到最大值,如下所示。

代碼:

import java.lang.Math;
import java.util.Scanner;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x, y, result;
x = -10;
y = -20;
result = Math.max(x,y);
//Print the maximum number between x and y of lower magnitude
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
登入後複製

輸出:

Java 最大()

如上面的程式碼所示,x 和y 是兩個變量,分別聲明並初始化為負值-10 和-20 值,稍後,呼叫max() 函數並將這些變數作為參數傳遞,因此結果為該函數為-10,其幅度較低。

Example #5

We write the java code for max() function where we use the max() function to pass positive and negative values and find the maximum, as below.

Code:

import java.lang.Math;
import java.util.Scanner;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x, y, result;
x = 10;
y = -20;
result = Math.max(x,y);
//Print the maximum number between x and y
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
登入後複製

Output :

Java 最大()

As in the above code, the x and y are two variables declare and initialize with values 10 and -20 values respectively, and the result of this function is 10.

Conclusion

The java max() function which is a built-in function in Java.lang.math class is used to gets the maximum of two numerical values, as we have seen above with an example.

以上是Java 最大()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!