2

我创建了一个名为的 Java 类Rectangle,它具有两个实例变量(宽度和高度)和两个实例方法(面积和周长),这两个方法都不接受参数,而是返回双精度值。area 方法返回矩形的面积(宽*高),而圆周返回(2*宽+2*高)。然后使用 main 方法创建 Demo 类,通过实例化 4 个对象来测试 Rectangle 类,并提示用户输入每个实例的宽度和高度。然后打印出每个实例的面积和周长。

我创建了两个类,第一个类是 Rectangle :

public class Rectagle {

    private double width;
    private double height;

    public double area() {
        return width * height;
    }

    public double circumference() {
        return 2*width+2*height;
    }
}

我创建了第二个类 Demo 来测试这个类:

import java.util.Scanner;
public class Demo {
    public static void main(String []args){
        Scanner console=new Scanner(System.in);
    Rectagle R1=new Rectagle();
    Rectagle R2=new Rectagle();
    Rectagle R3=new Rectagle();
    Rectagle R4=new Rectagle();

    }
}

我的问题,我不明白这一点”并提示用户输入每个实例的宽度和高度。然后打印出每个实例的面积和周长。

4

3 回答 3

1

您的构造函数没有参数。没有办法为宽度和高度分配一个值。

我建议你有这种构造函数

public Rectangle(double w, double h){
     width = w;
     height = h;
}

并以这种方式使用它:

 Rectagle R1=new Rectagle(30.0, 40.0);

或者,如果您需要,为您的实例变量添加一个 setter 和 getter,如下所示:

public void setWidth(double w){
   width = w
}

public double getWidth(){
   return width;
}

现在你的课已经完成了。请参阅正确使用Scanner类以了解如何从控制台读取。例如阅读:How to read integer value from the standard input in Java

于 2012-12-22T08:13:30.607 回答
1

愿这对你有帮助

public class Rectangle {

    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double getArea() {
        return width * height;
    }

    public double getCircumference() {
        return 2*width+2*height;
    }

    @Override
    public String toString() {
        return "Rectangle["+width+","+height+"]Area:"+getArea()+",Circumference:"+getCircumference();
    }

    public static void main(String[] args) {
         Scanner console=new Scanner(System.in);
        double width = getValue(console, "Width");
        double height = getValue(console, "Height");
        Rectangle rectangle = new Rectangle(width, height);
        System.out.println(rectangle);

    }

    public static double getValue(Scanner console, String name) {
        System.out.println("Enter "+name + " : ");
        String widthStr = console.nextLine();
        double parseDouble;
        try {
            parseDouble = Double.parseDouble(widthStr);
        }catch(NumberFormatException ne) {
            System.out.println("Unable to parse your input, enter correct value ");
            return getValue(console, name);
        }
        return parseDouble;
    }
}
于 2012-12-22T08:34:34.660 回答
0

公共类矩形{

private double width;
private double height;

public Rectangle(double width, double height) {
    this.width = width;
    this.height = height;
}

public double getArea() {
    return width * height;
}

public double getCircumference() {
    return 2*width+2*height;
}

@Override
public String toString() {
    return "Rectangle["+width+","+height+"]Area:"+getArea()+",Circumference:"+getCircumference();
}

public static void main(String[] args) {
     Scanner console=new Scanner(System.in);
    double width = getValue(console, "Width");
    double height = getValue(console, "Height");
    Rectangle rectangle = new Rectangle(width, height);
    System.out.println(rectangle);
}

public static double getValue(Scanner console, String name) {
    System.out.println("Enter "+name + " : ");
    String widthStr = console.nextLine();
    double parseDouble;
    try {
        parseDouble = Double.parseDouble(widthStr);
    }catch(NumberFormatException ne) {
        System.out.println("Unable to parse your input, enter correct value ");
        return getValue(console, name);
    }
    return parseDouble;
}

}

于 2021-01-12T15:13:17.117 回答