在Java中,你可以使用Math
類來調用各種數(shù)學函數(shù)。Math
類包含了許多靜態(tài)方法,用于執(zhí)行基本的數(shù)學運算,如加法、減法、乘法、除法、取平方根等。
以下是一些使用Math
類的示例:
public class Main {
public static void main(String[] args) {
double a = 10;
double b = 20;
double sum = Math.add(a, b);
System.out.println("Sum: " + sum);
}
}
public class Main {
public static void main(String[] args) {
double a = 10;
double b = 20;
double difference = Math.subtract(a, b);
System.out.println("Difference: " + difference);
}
}
public class Main {
public static void main(String[] args) {
double a = 10;
double b = 20;
double product = Math.multiply(a, b);
System.out.println("Product: " + product);
}
}
public class Main {
public static void main(String[] args) {
double a = 10;
double b = 20;
double quotient = Math.divide(a, b);
System.out.println("Quotient: " + quotient);
}
}
public class Main {
public static void main(String[] args) {
double number = 25;
double squareRoot = Math.sqrt(number);
System.out.println("Square root of " + number + " is: " + squareRoot);
}
}
這些示例展示了如何使用Math
類執(zhí)行基本的數(shù)學運算。請注意,Math
類中的所有方法都是靜態(tài)的,因此你可以直接通過類名調用它們,而無需創(chuàng)建類的實例。