java 中的不兼容类型

incompatible types in java

我不断收到这些恼人的错误....

Figures.java:106: error: incompatible types: possible lossy conversion from double to int
    Rectangle figure = new Rectangle(width, length, x, y);
                                     ^
Figures.java:116: error: incompatible types: possible lossy conversion from double to int
           s        = getDimension("side");

...即使我将长度和宽度设置为双打,getDimension 是双打,并且在 Rectangle.java 文件中,我将它们设置为双打。

这里是矩形 class

// ---------------------------------
// File Description:
//   Defines a Rectangle
// ---------------------------------

public class Rectangle extends Point
{
  private int    x, y;  // Coordinates of the Point
  private double length, width;

  public Rectangle(int x, int y, double l, double w)
  {
    super(x, y);
    length = l;
    width = w;
  }

  public int       getX()         {return x;}
  public int       getY()         {return y;}
  public double    getLength()    {return length;}
  public double    getWidth()     {return width;}

  public double area()      {return length * width;}
  public String toString()  {return "[" + x + ", " + y + "]" + " Length = " + length + " Width = " + width;}
}

这是我的主要测试文件,Figures.java 其中大部分工作已完成

// ---------------------------------
// Problem Description:
//   Create geometric figures
// ---------------------------------

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class Figures extends JApplet implements ActionListener
{
  private static final int POINT     = 0,  // JButton IDs
                           CIRCLE    = 1,
                           CYLINDER  = 2,
                           RECTANGLE = 3,
                           CUBE      = 4,
                           SQUARE    = 5;
  private static final DecimalFormat precision2 = new DecimalFormat("0.00");

  private JButton[] f_buttons;
  private String[]  figures = {"Point", "Circle", "Cylinder",
                               "Rectangle", "Cube", "Square"};

  public void init()
  {
    Container c = getContentPane();

    c.setLayout(new FlowLayout());
    f_buttons = new JButton[figures.length];
    for (int i = 0; i < figures.length; i++)
    {
      c.add(f_buttons[i] = new JButton(figures[i]));
      f_buttons[i].addActionListener(this);
    }
  }

  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == f_buttons[POINT])
      createPoint();
    else if (e.getSource() == f_buttons[CIRCLE])
      createCircle();
    else if (e.getSource() == f_buttons[CYLINDER])
      createCylinder();
    else if (e.getSource() == f_buttons[RECTANGLE])
      createRectangle();
    else if (e.getSource() == f_buttons[CUBE])
      createCube();
    else if (e.getSource() == f_buttons[SQUARE])
     createSquare();

  }

  private int getCoordinate(String msg)
  {
    String s= JOptionPane.showInputDialog("Enter " + msg + " coordinate");

    return Integer.parseInt(s);
  }

  private double getDimension(String msg)
  {
    String s= JOptionPane.showInputDialog("Enter " + msg);

    return Double.parseDouble(s);
  }

  private void createPoint()
  {
    Point figure = new Point(getCoordinate("x"), getCoordinate("y"));

    showStatus(figures[POINT] + ": " + figure.toString());
  }

  private void createCircle()
  {
    int    x      = getCoordinate("x"),
           y      = getCoordinate("y");
    double radius = getDimension("radius");
    Circle figure = new Circle(radius, x, y);

    showStatus(figures[CIRCLE] + ": " + figure.toString() +
               "; Area = " + precision2.format(figure.area()));
  }

  private void createCylinder()
  {
    int    x        = getCoordinate("x"),
           y        = getCoordinate("y");
    double radius   = getDimension("radius"),
           height   = getDimension("height");
    Cylinder figure = new Cylinder(height, radius, x, y);

    showStatus(figures[CYLINDER] + ": " + figure.toString() +
               "; Area = " + precision2.format(figure.area()) +
               "; Volume = " + precision2.format(figure.volume()));
  }

  private void createRectangle()
  {
    int    x        = getCoordinate("x"),
           y        = getCoordinate("y");
    double length   = getDimension("length"),
           width    = getDimension("width");
    Rectangle figure = new Rectangle(length, width, x, y);

    showStatus(figures[RECTANGLE] + ": " + figure.toString() +
               "; Area = " + precision2.format(figure.area()));
  }

    private void createCube()
  {
    int    x        = getCoordinate("x"),
           y        = getCoordinate("y"),
           s        = getDimension("side");
    double radius   = getDimension("radius"),
           height   = getDimension("height");
    Cube figure = new Cube(height, radius, x, y);

    showStatus(figures[CUBE] + ": " + figure.toString() +
               "; Area = " + precision2.format(figure.area()) +
               "; Volume = " + precision2.format(figure.volume()));
  }

    private void createSquare()
  {
    int    x        = getCoordinate("x"),
           y        = getCoordinate("y");
    double radius   = getDimension("radius"),
           height   = getDimension("height");
    Square figure = new Square(height, radius, x, y);

    showStatus(figures[SQUARE] + ": " + figure.toString() +
               "; Area = " + precision2.format(figure.area()) +
               "; Volume = " + precision2.format(figure.volume()));
  }


}
   new Rectangle(width, length, x, y);
       ...
public Rectangle(int x, int y, double l, double w)

其中一个与另一个不同。重新检查订单。

int ...           
       s        = getDimension("side");
...
private double getDimension(String msg)

sint,被分配了一个 double 值。报错信息其实很好的描述了问题。

在 createRectangle 中你做

int    x        = getCoordinate("x"),
       y        = getCoordinate("y");
double length   = getDimension("length"),
       width    = getDimension("width");
Rectangle figure = new Rectangle(length, width, x, y);

(新矩形(双精度、双精度、整数、整数))

Rectangle 的构造函数是

public Rectangle(int x, int y, double l, double w)

(整数,整数,双精度,双精度)