我正在做一个计算不同容器(长方体,圆柱体,球体)的体积,平均体积和标准偏差的项目
我为每个容器和抽象类(Shape)编写了一个类。在包含main方法的类中,我使用了arrayList来添加每个容器,但实际上我遇到的问题是我做了一个循环来提示用户他/她有哪种容器,在响应后,我可以调用右边的构造函数,但在输入第一个容器的所有内容后,循环终止。
我的主要方法代码是:
import java.util.*;
class testShape
{
public static void main(String args [])
throws java.io.IOException
{
double total = 0;
int i,n;
double deviation,average,sdeviation;
double sd = 0;
double s = 0;
Scanner sc = new Scanner(System.in);
ArrayList<Shape> shapelist = new ArrayList<Shape>();
for(i=0;i<3;i++)
{
System.out.println("Enter shape: B for box,C for cylinder, S for sphere ");
char c=(char)System.in.read();
if((c=='B')||(c=='b'))
{
System.out.println("How many boxes do you have");
n = sc.nextInt();
for(i=0;i<n;i++){
shapelist.add(new Box());
}
for(i=0;i<shapelist.size();i++)
{
System.out.println("--------"+shapelist.get(i).getShapeName() + "----------");
System.out.println(shapelist.get(i).toString());
System.out.println("The volume of "+shapelist.get(i).getShapeName()+""+(i+1)+" is "+shapelist.get(i).findVolume());
total = total+ shapelist.get(i).findVolume() ;
}
System.out.println("-------------------\n");
System.out.println("the total volume of"+shapelist.get(0).getShapeName()+" is: "+total+"\n");
average= total/n;
System.out.println("The average volume of "+shapelist.get(0).getShapeName()+" is: "+average);
for(i=0;i<n;i++){
deviation = (shapelist.get(i).findVolume()-average);
s= deviation*deviation;
sd = s+sd;
}
sdeviation =Math.sqrt(sd/n);
System.out.println("the standard deviation is: "+sdeviation);
}
else if((c=='C')||(c=='c')){
System.out.println("How many cylinders do you have");
n = sc.nextInt();
for(i=0;i<n;i++){
shapelist.add(new Cylinder());
}
for(i=0;i<shapelist.size();i++){
System.out.println("--------"+shapelist.get(i).getShapeName() + "----------");
System.out.println(shapelist.get(i).toString());
System.out.println("The volume of "+shapelist.get(i).getShapeName()+""+(i+1)+" is "+shapelist.get(i).findVolume());
total = total+ shapelist.get(i).findVolume() ;
}
System.out.println("-------------------\n");
System.out.println("the total volume of"+shapelist.get(0).getShapeName()+" is: "+total+"\n");
average= total/n;
System.out.println("The average volume of "+shapelist.get(0).getShapeName()+" is: "+average);
for(i=0;i<n;i++){
deviation = (shapelist.get(i).findVolume()-average);
s= deviation*deviation;
sd = s+sd;
}
sdeviation =Math.sqrt(sd/n);
System.out.println("the standard deviation is: "+sdeviation);
}
else if((c=='s')||(c=='S')){
System.out.println("How many Sphers do you have");
n = sc.nextInt();
for(i=0;i<n;i++){
shapelist.add(new Sphere());
}
for(i=0;i<shapelist.size();i++){
System.out.println("--------"+shapelist.get(i).getShapeName() + "----------");
System.out.println(shapelist.get(i).toString());
System.out.println("The volume of "+shapelist.get(i).getShapeName()+""+(i+1)+" is "+shapelist.get(i).findVolume());
total = total+ shapelist.get(i).findVolume() ;
}
System.out.println("-------------------\n");
System.out.println("the total volume of"+shapelist.get(0).getShapeName()+" is: "+total+"\n");
average= total/n;
System.out.println("The average volume of "+shapelist.get(0).getShapeName()+" is: "+average);
for(i=0;i<n;i++){
deviation = (shapelist.get(i).findVolume()-average);
s= deviation*deviation;
sd = s+sd;
}
sdeviation =Math.sqrt(sd/n);
System.out.println("the standard deviation is: "+sdeviation);
}
else
System.out.println("Invalid selection");
}
}
}