计算Java中的面积;运算顺序

huangapple go评论106阅读模式
英文:

Calculating Area in Java; Order of Operations

问题

我正在尝试使用以下公式找到多边形的面积:

面积 = r^2 n sin( 2 π / n) / 2

其中 n 是边数,r 是半径。我认为我的代码产生的结果不正确。如果 n = 6,r = 4,我得到的面积是 24。我的代码如下:

  1. import java.math.*;
  2. public class RegularPolygon {
  3. private int n; // 顶点数
  4. private double r; // 多边形的半径
  5. /**
  6. * 这是完整的构造函数
  7. * @param n 是顶点数
  8. * @param r 是半径
  9. */
  10. public RegularPolygon(int n, double r) {
  11. super();
  12. this.n = n;
  13. this.r = r;
  14. }
  15. /**
  16. * 默认构造函数。将边数和半径设置为零
  17. */
  18. public RegularPolygon() {
  19. super();
  20. this.n = 0;
  21. this.r = 0;
  22. }
  23. // 对顶点数 "n" 和半径 "r" 的获取和设置方法。
  24. public int getN() {
  25. return n;
  26. }
  27. public void setN(int n) {
  28. this.n = n;
  29. }
  30. public double getR() {
  31. return r;
  32. }
  33. public void setR(double r) {
  34. this.r = r;
  35. }
  36. @Override
  37. public String toString() {
  38. return "RegularPolygon [n=" + n + ", r=" + r + "]";
  39. }
  40. public double area(){
  41. float area;
  42. // 该方法返回多边形的面积
  43. // 根据需要使用 Math.PI 和 Math.sin
  44. return area = (float) (Math.pow(r, 2)* n * ( Math.sin(Math.PI / n)/2));
  45. }

我不清楚我的操作顺序出了什么问题。

英文:

I'm trying to find the area of a Polygon using the following formula:

Area = r^2 n sin( 2 π / n) / 2

where n is the number of sides and r is the radius. I do not think my code is producing the correct result. If n= 6 and r = 4, i'm getting an area of 24. My code is as follows:

  1. import java.math.*;

public class RegularPolygon {

  1. private int n; // number of vertices
  2. private double r; //radius of the polygon
  3. /**
  4. * This is the full constructor
  5. * @param n is the number of vertices
  6. * @param r is the radius
  7. */
  8. public RegularPolygon(int n, double r) {
  9. super();
  10. this.n = n;
  11. this.r = r;
  12. }
  13. /**
  14. * default constructor. Sets number of sides and radius to zero
  15. */
  16. public RegularPolygon() {
  17. super();
  18. this.n = 0;
  19. this.r = 0;
  20. }
  21. //getters and setters for Number of vertices "n" and radius "r".
  22. public int getN() {
  23. return n;
  24. }
  25. public void setN(int n) {
  26. this.n = n;
  27. }
  28. public double getR() {
  29. return r;
  30. }
  31. public void setR(double r) {
  32. this.r = r;
  33. }
  34. @Override
  35. public String toString() {
  36. return "RegularPolygon [n=" + n + ", r=" + r + "]";
  37. }
  38. public double area(){
  39. float area;
  40. //this method returns the area of the polygon
  41. //Use Math.PI and Math.sin as needed
  42. return area = (float) (Math.pow(r, 2)* n * ( Math.sin(Math.PI / n)/2));

It is unclear to me where my order of operations is messed up.

答案1

得分: 0

你没有正确翻译这个公式。你需要使用 return Math.pow(r, 2) * n * Math.sin(2 * Math.PI / n) / 2; 正如评论中所指出的,你漏掉了一个 2 并写成了 Math.sin(Math.PI / n)。这与操作顺序无关,因为这只涉及乘法和除法。

英文:

You're not translating the formula correctly. You need return Math.pow(r, 2) * n * Math.sin(2 * Math.PI / n) / 2; As forpas pointed out in the comments, you're missing a 2 and saying Math.sin(Math.PI / n) This has nothing to do with order of operations, since it's all just multiplication and division.

答案2

得分: 0

你不应该将类型转换为浮点型,因为你已经声明了方法的返回类型为双精度型。

  1. return Math.pow(r, 2) * n * Math.sin(2 * Math.PI/n) / 2;

我认为上述代码将满足你的需求。

英文:

You should not type cast to float as you have declared the return type of the method as double

  1. return Math.pow(r,2) * n * Math.sin(2 * Math.PI/n) / 2;

I think the above code will satisfy your need.

huangapple
  • 本文由 发表于 2020年5月5日 01:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61598667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定