I am trying to make a program with two methods which calculate and return the surface area and volume of a cylinder.
我正在嘗試使用兩種方法制作程序,這兩種方法可以計算並返回圓柱體的表面積和體積。
When i run the program the system outputs 0.00 for both and i'm unsure of what i'm doing wrong.
當我運行程序時,系統輸出0.00兩者,我不確定我做錯了什么。
Here is my code:
這是我的代碼:
public class Circle {
public static int height;
public static int radius;
public static double pi = 3.14;
public Circle(int height, int radius) {
height = 10;
radius = 5;
}
public static double getSurfaceArea() {
int radiusSquared = radius * radius;
double surfaceArea = 2 * pi * radius * height + 2 * pi * radiusSquared;
return surfaceArea;
}
public static double getVolume() {
int radiusSquared = radius * radius;
double volume = pi * radiusSquared * height;
return volume;
}
public static void main(String[] args) {
System.out.println("The volume of the soda can is: " + getVolume() + ".");
System.out.println("The surface area of the soda is: " + getSurfaceArea() + ".");
}
}
Thanks in advance.
提前致謝。
You have to add this line of code to your main:
您必須將這行代碼添加到您的主要:
Circle c = new Circle(10,5);
so it would be like so:
所以它會是這樣的:
public static void main(String[] args) {
Circle c = new Circle(10,5);
System.out.println("The volume of the soda can is: " + c.getVolume() + ".");
System.out.println("The surface area of the soda is: " + c.getSurfaceArea() + ".");
}
and change your circle constructor method to this:
並將您的圓構造函數方法更改為:
public Circle(int height, int radius) {
this.height = height;
this.radius = radius;
}
I believe this is what you are looking for:
我相信這就是你要找的東西:
public class Circle {
public static int height;
public static int radius;
public static double pi = 3.14;
public Circle(int height, int radius) {
this.height = height;
this.radius = radius;
}
public static double getSurfaceArea() {
int radiusSquared = radius * radius;
double surfaceArea = 2 * pi * radius * height + 2 * pi * radiusSquared;
return surfaceArea;
}
public static double getVolume() {
int radiusSquared = radius * radius;
double volume = pi * radiusSquared * height;
return volume;
}
public static void main(String[] args) {
Circle circle = new Circle(10,5);
System.out.println("The volume of the soda can is: " + circle.getVolume() + ".");
System.out.println("The surface area of the soda is: " + cirlce.getSurfaceArea() + ".");
}
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/10/83d70fd58d04692c0adb9f24d6e75f56.html。