当前位置:编程学习 > JAVA >>

java , 用单元格计算圆环的面积, 使用FOR 循环的~求高手啊!!有伪代码,求怎么用java写~

BEGIN Area of Annulus Set gridSize = 20 (we could use 20 by 20 grid cells) Get r1 from user (outer radius) Get r2 from user (inner radius) Set minx = -r1 Set maxx = r1 Set miny = -r1 Set maxy = r1 Set counter = 0 (will count how many grid cells are on the annulus) For column = 0 to gridSize-1 Set x = minx + (column + 0.5)*((maxx–minx)/gridSize) For row = 0 to gridSize-1 Set y = miny + (row + 0.5)*((maxy–miny)/gridSize) Set test = x*x + y*y if test < r1*r1 and test > r2*r2 Set counter = counter + 1 Set area = (maxx-minx)*(maxy-miny)*counter/(gridSize * gridSize) Output “Area : ” area END Area of Annulus
补充:高手快来啊。。。求助 求助!!!!!!
答案:现在的好多了

public class TestManager {
	public static void main(String[] args) {
		areaOfAnnulus(6.5, 3.2);
		areaOfAnnulus2(6.5, 3.2);
                                areaOfAnnulus(26.5, 3.2);
		areaOfAnnulus2(26.5, 3.2);
	}

	/**
	 * 用单元格计算圆环的面积
	 * 
	 * @param r1
	 *            外圆半径
	 * @param r2
	 *            内圆半径
	 */
	public static void areaOfAnnulus(double r1, double r2) {
//		int gridSize = 20;
		double minx = -r1;
		double maxx = r1;
		double miny = -r1;
		double maxy = r1;
		long gridSize = Math.round(r1/0.5);
		int counter = 0;
		for (int column = 0; column <= gridSize-1; column++) {
			double x = minx + (column + 0.5) * ((maxx - minx) / gridSize);
			for (int row = 0; row <= gridSize-1; row++) {
				double y = miny + (row + 0.5) * ((maxy - miny) / gridSize);
				double test = x * x + y * y;
				if (test < r1 * r1 && test > r2 * r2) {
					counter++;
				}
			}
		}
		double area = (maxx - minx) * (maxy - miny) * counter
				/ (gridSize * gridSize);
		System.out.println("area:" + area);
	}
	
	/**
	 * 外圆减内圆面积
	 * @param r1 外圆半径
	 * @param r2 内圆半径
	 */
	public static void areaOfAnnulus2(double r1, double r2) {
		System.out.println("area:" + Math.PI*(r1*r1-r2*r2));
	}
}

上一个:java代码纠错! 能编译成功,可是运行不了! 我的目的是做个动圆!在线等待!
下一个:求java高手,总是出现“需要为class,inte易做图ce或enum” 急急急

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,