java数学类

常用的数学类

System.out.println(Math.pow(3, 3)); // 3的3次方
System.out.println(Math.abs(-12)); // 12
System.out.println(Math.log(2)); // 以e为底, 2的对数
System.out.println(Math.max(12, 33)); // 求大数   33
System.out.println(Math.min(55, 33)); // 求小数  33
System.out.println(Math.round(3.56789));  // 4 四舍五入
System.out.println(Math.sqrt(3));  // 根号9  1.732

例子:解一元二次不等式

package com.xyq.math;
import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入a:");
        double a = sc.nextInt();
        System.out.println("请输入b:");
        double b = sc.nextInt();
        System.out.println("请输入c:");
        double c = sc.nextInt();
        // 先判断b*b-4*a*c是否大于0 . 不大于0无解
        double dt = b*b - 4*a*c;
        if (dt < 0){
            System.out.println("此方程无解");
        } else if(dt == 0){
            System.out.println("此方程有一个解");
            double x = -b/(2*a);
            System.out.println("x = " + x);
        } else {
            System.out.println("此方程有两个解");
            double x1 = (-b+Math.sqrt(dt)) / (2*a);
            double x2 = (-b-Math.sqrt(dt)) / (2*a);
            System.out.println("x1 = "+x1 + ", x2 = "+x2);
        }
    }
}
Last modification:October 23rd, 2019 at 10:56 pm

Leave a Comment