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.

提前致谢。

2 个解决方案

#1


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

更多相关文章

  1. 字体图标的引入和通过媒体查询改变导航样式
  2. HTML样式和常用选择器
  3. 字体图标的引用和自定义样式/媒体查询的使用
  4. 数据库的CURD操作、PDO本质与原理的学习
  5. CSS之伪类选择器和简单盒子简单案例
  6. 伪类选择器与盒模型常用属性
  7. 伪类选择器-结构伪类、根据位置选择匹配
  8. 7.4——常用标签与应用场景之表格与单元格
  9. css伪类选择器和盒模型

随机推荐

  1. Linux环境都没有,怎么学编程?憋说了,肝!(保姆
  2. 006. Z 字形变换 | Leetcode题解
  3. eruda 一个被人遗忘的调试神器
  4. Linux性能优化(五)——性能监控工具
  5. 小姐姐对不喜欢的男孩到底有多残忍?
  6. 007. 整数反转 | Leetcode题解
  7. 专属开发者的福利,1000刀+的羊毛在等你
  8. 008. 字符串转换整数 (atoi) | Leetcode
  9. 010. 正则表达式匹配 | Leetcode题解
  10. Linux 目录