java 参数传入 圆的周长和面积

来源:百度知道 编辑:UC知道 时间:2024/05/24 13:26:57
用java语言编写一个程序,题目如下:根据半径求圆的周长和面积并输出,要求圆的半径在运行时作为参数传入。(提示:要求圆的半径为double类型;字符串str转换为double类型数据方法为Double.parseDouble(str))。
我花了好长时间,总是出错,帮忙解决一下吧!!谢谢啦!
我后来又编出来了,而且正确哦!嘿嘿
public class yuan{
public static void main(String[] args)
{double r,c,s;
r=Double.parseDouble(args[0]);
c=2*Math.PI*r;
s=Math.PI*r*r;
System.out.println("c="+c);
System.out.println("s="+s);
}}

public class Test{
public static void main(String[] args){
Test t = new Test();
String d = "5";
double girth = t.roundGirth(d);
System.out.println ("周长是: " + girth);
double area = t.roundArea(d);
System.out.println ("面积是: " + area);
}

private double roundGirth(String str){
double d = 0;//半径
double roundGirth = 0;
d = Double.parseDouble(str);
roundGirth = 2 * d * 3.14;
return roundGirth;
}

private double roundArea(String str){
double d = 0;//半径
double roundArea = 0;
d = Double.parseDouble(str);
roundArea = d * d * 3.14;
return roundArea;
}
}