-6

我如何将精确值返回到计算中,即:4 * sqrt(3)与十进制形式相反?

编辑:对不起,我认为这是一个基本问题。这是我的代码:

package physics;
import java.util.Scanner;

public class Physics {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("do you want to \n [1] find the magnitude from components \n [2] find the components from magnitude and angle \n [3] Dot product from magnitude" + 
            "\n [4] Gravitational problems" );
    int key = in.nextInt();
    switch(key){
        case 1:
            compsToMag();
            break;
        case 2:
            magToComps();
            break;

        case 3:
            dotProductWithMags();
            break;
        case 4:
            gravity();
            break;
    } 
}

public static double compsToMag(){
    Scanner input = new Scanner(System.in);
    System.out.println("enter x, [Enter], then y comp");
    double x = input.nextDouble();
    double y = input.nextDouble();

    double mag = Math.sqrt((x*x) + (y*y));

    System.out.print("Magnitude = " + mag);
    return mag;

}

public static void magToComps(){
    Scanner input = new Scanner(System.in);
    System.out.println("enter magnitude to find components");
    int magnitude = input.nextInt();
    System.out.println("enter angle in degrees");
    double angle = input.nextDouble();

    double x = magnitude * (Math.cos(angle));
    double y = magnitude * (Math.sin(angle));

    System.out.println("X = " + x + " \n Y = " + y);
}

public static double dotProductWithMags(){
    Scanner input = new Scanner(System.in);
    System.out.println("enter magnitude of vector 1");
    int mag1 = input.nextInt();
    System.out.println("enter magnitude of vector 2");
    int mag2 = input.nextInt();
    System.out.println("enter angle");
    double angle = input.nextDouble();

    double result = mag1 * mag2 * Math.cos(angle);

    System.out.print("dot product result = " + result);
    return result;
}

public static void gravity(){
           Scanner input = new Scanner(System.in);

   double G = 6.674 * Math.pow(10, (-11));


   System.out.println("Mass 1 'base' [base * 10^(exponent)]:");
   double mass1 = input.nextDouble();

    System.out.println("Mass 1 'exponent' [base * 10^(exponent)]:");
    double mass1Exp = input.nextDouble();

    System.out.println("");

    System.out.println("Mass 2 'base':");
    double mass2 = input.nextDouble();

    System.out.println("Mass 2 'exponent':");
    double mass2Exp = input.nextDouble();

    System.out.println("");

    System.out.println("radius/distance apart:");
    double rad = input.nextDouble();

    double force = (G * (mass1 * Math.pow(10, mass1Exp)) * (mass2 * Math.pow(10, mass2Exp))) / (Math.pow(rad, 2));

    double accelerationMass1 = force/mass1;
    double accelerationMass2 = force/mass2;

    System.out.println("Force of gravity = " + force);
    System.out.println("Acceleration of mass1 = " + accelerationMass1);
    System.out.println("Acceleration of mass2 = " + accelerationMass2);
}

}

我只想能够返回一个带有平方根符号而不是小数的精确值

4

1 回答 1

2

如果您需要确切的值。您应该考虑使用提供符号计算的数学库

看看这个问题

于 2013-09-10T18:41:59.267 回答