【问题标题】:Java Class Rectangle and test it using demo classJava Class Rectangle 并使用演示类对其进行测试
【发布时间】:2012-12-09 16:25:39
【问题描述】:

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

我创建了两个类,第一个类是 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();

    }
}

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

【问题讨论】:

  • 你的班级被介绍给java.util.Scanner了吗?
  • 我必须放 Scanner ,因为我应该要求用户使用键盘输入值

标签: java


【解决方案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;
    }
}

【讨论】:

  • vels4j ,拜托我还是没有覆盖,你写的很多东西我不明白
  • 哪一个你看不懂?
【解决方案2】:

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

我建议你有这种构造函数

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

【讨论】:

  • 我可以使用这种方式,但我必须提示用户输入宽度和高度......而不是在构造函数中输入值
  • @trapo 我认为 OP 的问题是用户输入。使用 setter 并不能解决他的问题,因为他不想要硬编码的值,他想要用户输入。
  • @jlordo:是的,第一条评论更好地解释了这一点,但如果不修复课程,它将永远无法工作。我为问题的第二部分添加了参考。
【解决方案3】:

公共类矩形{

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;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多