矩形自定义对象显示零宽度,应为非零

时间:2014-06-06 02:31:06

标签: java

我写了一个Rectangle类,它有字段的长度和宽度。方法的setLength,setWidth,getLength,getWidth,getPerimeter和getArea。在我的主程序中,RectangleTest;我跑的时候;我可以输入长度和宽度。然后程序显示我为长度输入的值(这是正确的),但是当它显示我为宽度输入的值时;它总是显示0.0。请帮忙。这是源代码:

package com.delgado;

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class RectangleTest {

    public static void main(String[] args) {
        double length;
        double width;
        String input;
        DecimalFormat formatter = new DecimalFormat("#0.0");

        input = JOptionPane
                .showInputDialog("Please enter the length of the basketball court: ");
        length = Double.parseDouble(input);
        input = JOptionPane
                .showInputDialog("Please enter the width of the basketball court: ");
        width = Double.parseDouble(input);
        Rectangle basketBall = new Rectangle(length, width);
        JOptionPane.showMessageDialog(
                null,
                "You entered " + formatter.format(basketBall.getLength())
                        + " for the length, and "
                        + formatter.format(basketBall.getWidth())
                        + " for the width.");

    }

}

如果您需要Rectangle类的源代码,请告诉我。谢谢你们。

以下是Rectangle类的源代码:

package com.delgado;

/* This Class creates an object that takes the length and width as arguments,
 * and returns the perimeter and area of a rectangle.
 */

public class Rectangle {

    private double length; // Holds the length.
    private double width; // Holds the width.

    /**
     * This is a default constructor
     */

    public Rectangle() {

    }

    /**
     * This is a constructor that takes two arguments; len and w.
     * 
     * @param len
     *            The length of the rectangle.
     * @param w
     *            The width of the rectangle.
     */
    public Rectangle(double len, double w) {
        length = len;
        w = width;
    }

    /**
     * The method setLength sets the length of a rectangle.
     * 
     * @param len
     *            The length of a rectangle.
     */

    public void setLength(double len) {
        length = len;
    }

    /**
     * The method setWidth sets the width of a rectangle.
     * 
     * @param w
     *            The width of a rectangle.
     */
    public void setWidth(double w) {
        width = w;
    }

    /**
     * The method getLength returns the length of a rectangle.
     * 
     * @return Returns the length of a rectangle.
     */
    public double getLength() {
        return length;
    }

    /**
     * The method getWidth returns the width of a rectangle.
     * 
     * @return Returns the width of a rectangle.
     */
    public double getWidth() {
        return width;
    }

    /**
     * The method getArea returns the area of a rectangle.
     * 
     * @return Returns the area of a rectangle.
     */
    public double getArea() {
        return length * width;
    }

    /**
     * The method getPerimeter returns the perimeter of a rectangle.
     * 
     * @return Returns the perimeter of a rectangle.
     */
    public double getPerimeter() {
        return (length * 2) + (width * 2);
    }
}

1 个答案:

答案 0 :(得分:0)

我不得不怀疑你的JOptionPane是否正在弄乱你的扫描仪...我不能说我测试它,但无论如何,我会进一步简化。只要您显示JOptionPanes,谁不会显示一个显示消息同时输入的消息?

JOptionPane.showInputDialog(....)

修改: Yikes

public Rectangle(double len, double w) {
    length = len;
    w = width;
}

你在这里设置w参数,而不是宽度字段!相反它应该是:

   width = w; //*********

你明白为什么吗?

相关问题