我坚持不懈,我想创建一种将所有值(卷,区域,R,H)存储在升序顺序中的数组中的方法。请参阅我在下面的代码

发布于 2025-01-22 20:42:49 字数 1626 浏览 0 评论 0原文

我需要一种方法或方法,将所有值(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-01-29 20:42:49

下面的事情应该有效:

ArrayList<Double> shapeProperties = new ArrayList<Double>();
shapeProperties.add(r);
shapeProperties.add(h);
shapeProperties.add(area);
shapeProperties.add(volume);

Collections.sort(shapeProperties);

如果您想将其封装到一个函数中,则可以:

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;
}

这样称呼:
arrayList&lt; double&gt; properties = sortpropertiesinarray(R,H,Getarea(R,H),GetVolume(R,H));

示例输出:

Something like below should work:

ArrayList<Double> shapeProperties = new ArrayList<Double>();
shapeProperties.add(r);
shapeProperties.add(h);
shapeProperties.add(area);
shapeProperties.add(volume);

Collections.sort(shapeProperties);

If you want to encapsulate this into a function, you could:

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;
}

and call it as such:
ArrayList<Double> properties = sortPropertiesInArray(r, h, getArea(r,h), getVolume(r,h));

Example output:
image for the above code working as intended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文