2016-04-11 113 views
0

我不斷獲取這些惱人的錯誤....不兼容的類型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是雙,並且在矩形。 Java文件,我建立他們雙打。

這裏是Rectangle類

// --------------------------------- 
// 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())); 
    } 


} 
+0

只需投射值。關鍵是您使用的方法接受整數,您提供了一個「double」。這意味着你會失去一些精度,因爲數字基本上會被截斷。 – Rogue

+0

改爲:'矩形圖=新的矩形(x,y,長度,寬度);' –

回答

0
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值。錯誤信息實際上非常擅長描述問題。

0

在createRectangle你做

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

(新的Rectangle(雙,雙,INT,INT))

其中爲矩形的構造是

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

(INT,INT ,雙倍,雙倍)