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

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

Calculating Area in Java; Order of Operations

问题

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

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

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

import java.math.*;

public class RegularPolygon {

	private int n; // 顶点数
	private double r; // 多边形的半径
	
	/**
	 * 这是完整的构造函数
	 * @param n 是顶点数
	 * @param r 是半径
	 */
	public RegularPolygon(int n, double r) {
		super();
		this.n = n;
		this.r = r;
	}
	
	/**
	 * 默认构造函数。将边数和半径设置为零
	 */
	public RegularPolygon() {
		super();
		this.n = 0;
		this.r = 0;
	}
	
	// 对顶点数 "n" 和半径 "r" 的获取和设置方法。

	public int getN() {
		return n;
	}

	public void setN(int n) {
		this.n = n;
	}

	public double getR() {
		return r;
	}

	public void setR(double r) {
		this.r = r;
	}

	@Override
	public String toString() {
		return "RegularPolygon [n=" + n + ", r=" + r + "]";
	}
	
	public double area(){
		float area;
		// 该方法返回多边形的面积
		// 根据需要使用 Math.PI 和 Math.sin
		return area =  (float) (Math.pow(r, 2)* n * ( Math.sin(Math.PI / n)/2));
}

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

英文:

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:

import java.math.*;

public class RegularPolygon {

private int n; // number of vertices
private double r; //radius of the polygon
/**
* This is the full constructor
* @param n is the number of vertices
* @param r is the radius
*/
public RegularPolygon(int n, double r) {
super();
this.n = n;
this.r = r;
}
/**
* default constructor.  Sets number of sides and radius to zero
*/
public RegularPolygon() {
super();
this.n = 0;
this.r = 0;
}
//getters and setters for Number of vertices "n" and radius "r".
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
@Override
public String toString() {
return "RegularPolygon [n=" + n + ", r=" + r + "]";
}
public double area(){
float area;
//this method returns the area of the polygon
//Use Math.PI and Math.sin as needed
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

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

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

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:

确定