将数学公式输入Java

时间:2018-06-30 16:12:50

标签: java

我遇到一个问题,我无法解决在互联网上进行搜索的问题,基本上,我需要用户首先输入球体的直径,然后再获取半径,然后使用公式(4乘以3乘以pi (3.14)乘以半径(由直径-用户输入)得出的3的幂。 而另一个非常相似..... 4乘以pi(3.14)乘以半径乘以2的幂。

现在的问题是,每次我尝试编译时,都会出现错误,指出该方法是有损double而不是int或无法识别符号。

任何帮助都将是有帮助的,因为我找不到解决方案。

代码如下:

import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
import java.lang.*;
import java.util.Scanner;
import java.util.*;

public class RadiusConverter {

    public class Pi{
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int FirstStep, Diameter, Radius, SecondStep, ThirdStep;

        System.out.println ("Please enter the diameter of the Sphere: ");

        int diameter =scan.nextInt();

        System.out.println ("Your Shpere has a radius of: " + FirstStep);

        int radius = scan.nextInt();

        System.out.println ("The Volume of your Sphere is: " + SecondStep);

        System.out.println("The Surface Area of your Sphere is: " + ThirdStep);

        FirstStep = Diameter / 2;
        SecondStep =  4 / 3 * 3.14 * (int) FirstStep * 3;
        ThirdStep = 4 * 3.14 * (int) Radius * 2;
        Diameter = diameter;
        Radius = radius;
        //------------------------------------------------------------------------
        //
        //
        //-------------------------------------------------------------------------
        scan.close();
    }
}

2 个答案:

答案 0 :(得分:0)

      This is the older source code I was using … which also did not work.


      import static java.lang.Math.sqrt;
      import static java.lang.Math.abs;
      import java.lang.*;
      import java.util.Scanner;
       import java.util.*;
      public class RadiusConverter
     {

  public class Pi{
  }
  public static void main(String[] args)
   {

     Scanner scan = new Scanner( System.in );

    double FirstStep, Diameter, Radius, SecondStep, ThirdStep;


 System.out.println ("Please enter the diameter of the Sphere: ");
 double diameter =scan.nextDouble();


 System.out.println ("Your Shpere has a radius of: " + FirstStep);
 double radius =scan.nextDouble();


 System.out.println ("The Volume of your Sphere is: " + SecondStep);

 System.out.println("The Surface Area of your Sphere is: " + ThirdStep);

 FirstStep = diameter / 2;
 SecondStep =  4 / 3 * Math.PI* Math.pow (FirstStep, 3);
 ThirdStep = 4 * Math.PI * radius * Math.pow (2);


//------------------------------------------------------------------------
//
//
//-------------------------------------------------------------------------

    scan.close();


}

}

答案 1 :(得分:0)

尝试这个

import java.util.Scanner;

public class RadiusConverter {

    public class Pi {
    }

    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            int firstStep = 0, diameter = 0, radius = 0, secondStep = 0, thirdStep = 0;
            System.out.print("Please enter the diameter of the Sphere: ");
            diameter = scan.nextInt();
            System.out.print("Please enter the radius of the Sphere: ");
            radius = scan.nextInt();

            firstStep = diameter / 2;
            secondStep = (int) (4 / 3 * 3.14 * firstStep * 3);
            thirdStep = (int) (4 * 3.14 * radius * 2);

            System.out.println("Your Shpere has a radius of: " + firstStep);
            System.out.println("The Volume of your Sphere is: " + secondStep);
            System.out.println("The Surface Area of your Sphere is: " + thirdStep);
        }
    }
}
  

输出

Please enter the diameter of the Sphere: 250
Please enter the radius of the Sphere: 25
Your Shpere has a radius of: 125
The Volume of your Sphere is: 1177
The Surface Area of your Sphere is: 628