wha*_*mii 1 java class tostring setvalue
我是Java的新手,正在完成一项任务.我应该创建我自己的类"Cylinder"并从中调用方法,以便能够打印出半径和高度,然后更改它并再次打印出来.我得到它来打印cylinder1的设定值和cylinder1.setRadius/setHeight..my问题是我必须打印像Cylinder Radius:和Height:...但是当它打印所有我得到的是3.04.0或者它设置的任何......我花了很长时间试图解决这个问题,欢迎任何帮助!
public class Funtimes{
public static void main(String[] args){
Cylinder cylinder1= new Cylinder(1,2);
System.out.println(cylinder1);
cylinder1.setRadius(3);
cylinder1.setHeight(4);
System.out.println(cylinder1);
}}
class Cylinder
{
private double radius, height, area, volume;
public Cylinder(double height, double radius){
this.radius = radius;
this.height=height;
}
public double getRadius() {
return radius;
}
public double getHeight() {
return height;
}
public double getArea() {
double area = (2 * Math.PI * radius * height) + (2 * Math.PI * Math.pow(radius, 2));
return area;
}
public void setRadius(double r) {
radius = r;
}
public void setHeight(double h) {
height = h;
}
public double calcVolume() {
double volume = Math.PI * Math.pow(radius, 2) * height;
return volume;
}
public String toString (){
StringBuilder StBuild = new StringBuilder();
StBuild.append(radius).append(height);
return StBuild.toString();
}
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(5, 5);
System.out.println(cylinder1);
}
}
Run Code Online (Sandbox Code Playgroud)
修改你的 toString()
public String toString () {
return String.format("Radius: %s Height: %s", radius, height);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |