矩形周长面积java代码 java求矩形面积和周长测试类

JAVA求助:由键盘输入一个矩形的长和宽,求出这个矩形的周长和面积 该怎么写

public static void main(String[] args) {

创新互联为客户提供专业的网站设计制作、成都网站建设、程序、域名、空间一条龙服务,提供基于WEB的系统开发. 服务项目涵盖了网页设计、网站程序开发、WEB系统开发、微信二次开发、手机网站制作设计等网站方面业务。

System.out.println("输入长度");

Double c=new Scanner(System.in).nextDouble();

System.out.println("输入宽度");

Double k=new Scanner(System.in).nextDouble();

System.out.println("面积:"+c*k);

System.out.println("周长:"+(c+k)*2);

}

急求 java编码 求矩形周长 面积 以及对角线长度

平方可以直接用乘法来实现,下面是我写的代码,你参考下,源代码如下:

/**

* 求矩形周长 面积 以及对角线长度

*

* @author johnston678

* @version 2011-1-17

*/

public class RectangleDemo {

//定义长,宽

private static double x=5.9823,y=6.7323;

//定义周长lengthOfGirth,面积area,对角线长度lengthOfDiagonal

private static double lengthOfGirth,area,lengthOfDiagonal;

public static void main(String[] args) {

lengthOfGirth = 2 * (x + y);

area = x * y;

lengthOfDiagonal = Math.sqrt(x * x + y * y); //对角线长度是长和宽的平方根

//格式化输出,类似C语句的输出

System.out.printf("矩形的长为:%.2f,宽为:%.2f,周长 为%.2f,面积为%.2f,对角线长度为%.2f",x,y,lengthOfGirth,area,lengthOfDiagonal);

}

}

输出结果为:

矩形的长为:5.98,宽为:6.73,周长 为25.43,面积为40.27,对角线长度为9.01

Java编写一个矩形类,并计算面积和周长?

class Rectangle{

private int width = 2;

private int length = 1;

public int getWidth(){

return this.width;

}

public void setWidth(int w){

this.width = w;

}

public int getLength(){

return this.length;

}

public void setLength(int l){

this.length = l;

}

public int getArea(){

return this.length * this.width;

}

public int getCircumference(){

return (this.length + this.width) * 2;

}

public Rectangle(){}

public Rectangle(int l, int w){

this.length = l;

this.width = w;

}

}

public class demo{

public static void main(String[] args) {

Rectangle rect = new Rectangle(30, 8);

System.out.print("长方形的面积是:");

System.out.println(rect.getArea());

System.out.printf("长方形的周长是:%d\n", rect.getCircumference());

}

}


文章标题:矩形周长面积java代码 java求矩形面积和周长测试类
URL标题:http://myzitong.com/article/hjedgg.html