主题:java 一道关于“计算多个圆环面积之和”的问题
偶是新手啦,觉得java很难,可是又不能不学。有题作业题,不是很会编。望各位路过的GG、MM们帮帮忙,thx!!
下面是我编的代码,可是不知道哪里错了!!
程序如下:
package totalarea;
/**
*
* @author Administrator
*/
public class TotalArea {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
GoodRing[] ringArray;
ringArray = createRingArray();
printRingArray(ringArray);
}
public static GoodRing[] createRingArray(){
GoodRing[] ringArray = new GoodRing[10];
for(int i=0;i<ringArray.length;i++){
ringArray[i] = new GoodRing(Math.random()*100);
}
return ringArray;
}
public static void printRingArray(GoodRing[] ringArray){
System.out.println("Radius1\t\t"+"Radius2\t\t"+"Area");
for (int i=0;i<ringArray.length;i++ ){
System.out.print(ringArray[i].getRadius1()+"\t\t"+ringArray[i].getRadius2()+"\t\t"+
ringArray[i].findArea()+'\n');
}
System.out.println("---------------- ");
System.out.println("The tatal areas of GoodRings is \t "+
sum(ringArray));
}
public static double sum(GoodRing[] ringArray ){
double sum = 0;
for(int i=0;i < ringArray.length;i++)
sum += ringArray[i].findArea();
return sum;
}
}
class GoodRing {
private double radius1,radius2;
public GoodRing(){
radius1 = 3.0;
radius2 = 2.0;
}
public GoodRing (double newRadius1,double newRadius2) {
radius1 = newRadius1;
radius2 = newRadius2;
}
GoodRing(double d) {
GoodRing[] ringArray = new GoodRing[10];
}
public double getRadius1(){
return radius1;
}
public double getRadius2(){
return radius2;
}
public void setRadius(double newRadius1,double newRadius2){
radius1 = (newRadius1 >= 0) ? newRadius1 : 0;
radius2 = (newRadius2 >= 0) ? newRadius2 : 0;
}
public double findArea(){
return (radius1*radius1-radius2*radius2)*Math.PI;
}
}
运行结果是:
init:
deps-jar:
Compiling 1 source file to E:\JAVA\TotalArea\build\classes
compile-single:
run-single:
Radius1 Radius2 Area
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
----------------
The tatal areas of GoodRings is 0.0
成功生成(总时间:3 秒)
下面是我编的代码,可是不知道哪里错了!!
程序如下:
package totalarea;
/**
*
* @author Administrator
*/
public class TotalArea {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
GoodRing[] ringArray;
ringArray = createRingArray();
printRingArray(ringArray);
}
public static GoodRing[] createRingArray(){
GoodRing[] ringArray = new GoodRing[10];
for(int i=0;i<ringArray.length;i++){
ringArray[i] = new GoodRing(Math.random()*100);
}
return ringArray;
}
public static void printRingArray(GoodRing[] ringArray){
System.out.println("Radius1\t\t"+"Radius2\t\t"+"Area");
for (int i=0;i<ringArray.length;i++ ){
System.out.print(ringArray[i].getRadius1()+"\t\t"+ringArray[i].getRadius2()+"\t\t"+
ringArray[i].findArea()+'\n');
}
System.out.println("---------------- ");
System.out.println("The tatal areas of GoodRings is \t "+
sum(ringArray));
}
public static double sum(GoodRing[] ringArray ){
double sum = 0;
for(int i=0;i < ringArray.length;i++)
sum += ringArray[i].findArea();
return sum;
}
}
class GoodRing {
private double radius1,radius2;
public GoodRing(){
radius1 = 3.0;
radius2 = 2.0;
}
public GoodRing (double newRadius1,double newRadius2) {
radius1 = newRadius1;
radius2 = newRadius2;
}
GoodRing(double d) {
GoodRing[] ringArray = new GoodRing[10];
}
public double getRadius1(){
return radius1;
}
public double getRadius2(){
return radius2;
}
public void setRadius(double newRadius1,double newRadius2){
radius1 = (newRadius1 >= 0) ? newRadius1 : 0;
radius2 = (newRadius2 >= 0) ? newRadius2 : 0;
}
public double findArea(){
return (radius1*radius1-radius2*radius2)*Math.PI;
}
}
运行结果是:
init:
deps-jar:
Compiling 1 source file to E:\JAVA\TotalArea\build\classes
compile-single:
run-single:
Radius1 Radius2 Area
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
----------------
The tatal areas of GoodRings is 0.0
成功生成(总时间:3 秒)