
正文
java长方形如何敲代码 用java代码画出长方形
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
编写java程序,输入一个长方体的长、宽、高,求长方体的表面积和体积,并将结果显示
1、长方体表面积公式 :S = 2(ab + bc + ac);
2、长方体体积公式 :V = abc = Sh;(这里的S表示底面积)。
实现如下:
public class Cuboid {
// 定义长方体的长、宽、高
private double length, width, height;
public Cuboid(double length, double width, double height) {
super();
this.length = length;
this.width = width;
this.height = height;
}
// 获取当前长方体的表面积
public double getSurface() {
return getSurface(length, width, height);
}
// 获取当前长方体的体积
public double getVolume() {
return getVolume(length, width, height);
}
// 计算长方体表面积的通用方法
public static double getSurface(double length, double width, double height) {
return 2 * (length * width + width * height + length * height);
}
// 计算长方体体积的通用方法
public static double getVolume(double length, double width, double height) {
return length * width * height;
}
public static void main(String[] args) {
// 1、创建长方体对象,计算当前长方体的表面积和体积
Cuboid cuboid = new Cuboid(1, 1.6, 4.8);
System.out.println(cuboid.getSurface());
System.out.println(cuboid.getVolume());
// 2、使用通用方法,计算任意长方体的表面积和体积
System.out.println(Cuboid.getSurface(1, 1.6, 4.8));
System.out.println(Cuboid.getVolume(1, 1.6, 4.8));
}
}
相关问答
Q1: java怎么编写求长方形表面积和体积的代码
//主要代码 System.out.print("长:") double num1=input.nextDouble(); //代码太多以后简写System...("宽:"); double num2=.... System....("高:")double num3=.... System...("表:"+(num1*num2+num1*num3+num2*num3)*2+" 体积:"+n1*n2*n3)//打字不容易求给分
Q2: java中做一个按钮,点击按钮后画一个矩形的代码怎么写?
兄弟帮java长方形如何敲代码你写java长方形如何敲代码了一个java长方形如何敲代码:
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Print {
public static void main(String[] args) {
new Te();
}
}
class Te extends Frame implements ActionListener {
Color cc = Color.red;
int x = -20, y = -50;
Random r = new Random();
public Te() {
this.setLayout(null);
Button b = new Button("画圆");
this.add(b);
b.setBounds(30,30,50,50);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter () {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setBounds(200,200,500,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
this.cc = Color.red;
this.x = r.nextInt(400);
do {
int x1 = r.nextInt(300);
this.y = x1;
} while (this.y 50);
this.repaint();
}
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(cc);
g.drawRect(x,y,50,50);
g.setColor(c);
}
}
Q3: java长方形
Scanner scanner = new Scanner(System.in);
System.out.println("请输入长方形的长:");
int inputWidth = scanner.nextInt();
System.out.println("请输入长方形的宽:");
int inputHeight = scanner.nextInt();
int area = inputWidth * inputHeight;
int perimeter = 2*(inputWidth+inputHeight);
System.out.println("宽为"+inputWidth+"高为"+inputHeight+"的长方形:");
System.out.println("面积为:"+area);
System.out.println("周长为:"+perimeter);
scanner.close();
Q4: 用java编程求长方形和正方形的面积
/**
*
* @author 摆渡凌云
*/
public class Main {
//应用举例
public static void main(String[] args) {
Rectangle rect = new Rectangle(13,34);
Rectangle square = new Rectangle(13);
System.out.println("长为: " + rect.getLength() +" 宽为:"+rect.getWidth() +" java长方形如何敲代码的长方形面积为:" + rect.getArea());
System.out.println("边长为:" + square.getLength() +" java长方形如何敲代码的正方形面积为:"+square.getArea());
//静态方法调用
System.out.println("长方形java长方形如何敲代码的面积" + Rectangle.getArea(23,34));
}
}
//长方形(正方形)类
class Rectangle {
private double length = 0.0; //长
private double width = 0.0; //宽
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
//构造长方形
public Rectangle(double length , double width) {
this.length = length;
this.width = width;
}
//构造正方形
public Rectangle(double side) {
this(side,side);
}
//获得面积方法
public double getArea()
{
return length * width;
}
//静态方法,可以直接调用返回长方形(正方形java长方形如何敲代码的面积)
//x 长方形的长java长方形如何敲代码, y长方形的宽
public static double getArea(double x,double y)
{
return x*y;
}
}
关于java长方形如何敲代码和用java代码画出长方形的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







