我坚持不懈,我想创建一种将所有值(卷,区域,R,H)存储在升序顺序中的数组中的方法。请参阅我在下面的代码
我需要一种方法或方法,将所有值(R,H,区域,音量)存储在下面的气缸类的数组中。该数组应以升序顺序包含值。这是以下代码以查找区域和音量。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Double> shapeProperties = new ArrayList<Double>();
double tc, r, h;
System.out.println("Enter # of test cases");
tc = sc.nextInt();
for (int i = 0; i < tc; i++) {
System.out.println("Enter base radius");
r = sc.nextInt();
System.out.println("Enter height");
h = sc.nextInt();
if (tc > 10 || r > 10 || h > 10) {
System.out.println("Please enter a value under 10");
break;
} else {
System.out.println("Radius r= " + r);
System.out.println("Height h= " + h);
System.out.println("Volume = " + getVolume(r, h));
System.out.println("Area = " + getArea(r, h));
ArrayList<Double> properties = sortPropertiesInArray(r, h, getArea(r,h), getVolume(r,h));
}
}
}
static ArrayList<Double> sortPropertiesInArray(double r, double h, double area, double volume) {
ArrayList<Double> shapeProperties = new ArrayList<Double>();
shapeProperties.add(r);
shapeProperties.add(h);
shapeProperties.add(area);
shapeProperties.add(volume);
Collections.sort(shapeProperties);
return shapeProperties;
}
public static double getVolume(double r, double h) {
return Math.PI * r * r * h;
}
public static double getArea(double r, double h) {
return 2 * Math.PI * r * (r * h);
}
}
I need a method or way to store all values(r,h,area,volume) in a array for the cylinder class below. The array should contain the values in in ascending order.This is the code below to find area and volume.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Double> shapeProperties = new ArrayList<Double>();
double tc, r, h;
System.out.println("Enter # of test cases");
tc = sc.nextInt();
for (int i = 0; i < tc; i++) {
System.out.println("Enter base radius");
r = sc.nextInt();
System.out.println("Enter height");
h = sc.nextInt();
if (tc > 10 || r > 10 || h > 10) {
System.out.println("Please enter a value under 10");
break;
} else {
System.out.println("Radius r= " + r);
System.out.println("Height h= " + h);
System.out.println("Volume = " + getVolume(r, h));
System.out.println("Area = " + getArea(r, h));
ArrayList<Double> properties = sortPropertiesInArray(r, h, getArea(r,h), getVolume(r,h));
}
}
}
static ArrayList<Double> sortPropertiesInArray(double r, double h, double area, double volume) {
ArrayList<Double> shapeProperties = new ArrayList<Double>();
shapeProperties.add(r);
shapeProperties.add(h);
shapeProperties.add(area);
shapeProperties.add(volume);
Collections.sort(shapeProperties);
return shapeProperties;
}
public static double getVolume(double r, double h) {
return Math.PI * r * r * h;
}
public static double getArea(double r, double h) {
return 2 * Math.PI * r * (r * h);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的事情应该有效:
如果您想将其封装到一个函数中,则可以:
这样称呼:
arrayList&lt; double&gt; properties = sortpropertiesinarray(R,H,Getarea(R,H),GetVolume(R,H));
示例输出:
Something like below should work:
If you want to encapsulate this into a function, you could:
and call it as such:
ArrayList<Double> properties = sortPropertiesInArray(r, h, getArea(r,h), getVolume(r,h));
Example output: