我定义了一个名为Rectange 的矩形类,包括的属性有矩形的左下角和右上角2个点的坐标,要求计算并打印宽(width),高(height),面积(area);我的的程序如下,但是打印出的结果跟我算的不一样,换了几组数据都是出错,不知道为什么?class Rectangle 
{
 int x1,y1,x2,y2;
 int width,height;


Rectangle(int x1,int y1,int x2,int y2)//构造函数
{
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
}
int getWidth()//计算宽width
{
width=x2-x1;
return width;
}
int getHeight()//计算高heigth
{
height=y2=y1;
return height;
}
int getArea()//计算面积
{
    
return (width*height);
}
public static void main(String []args)
{
Rectangle db1=new Rectangle (20,50,100,80);
System.out.println("width="+db1.getWidth());
        System.out.println("height="+db1.getHeight());
System.out.println("area="+db1.getArea());
}
}

解决方案 »

  1.   

    Rectangle db1=new Rectangle (20,50,100,80); 这行的数据大家可以改改,看看是不是也是错的;
    像上面的宽应该为80,高应该为30,面积为2400;但结果不是这样
      

  2.   

    int getHeight()//计算高heigth 

    height=y2=y1;    //请解释下 这句的用意
    return height; 

      

  3.   

    int getHeight()//计算高heigth 

    height=y2=y1; 
    return height; 
    } 呵呵,太不相信电脑了
      

  4.   

    我定义了一个名为Rectange 的矩形类,包括的属性有矩形的左下角和右上角2个点的坐标,要求计算并打印宽(width),高(height),面积(area); class Rectange {
      
        private int x,y,x1,y1;
        public Rectange(int x,int y,int x1,int y1) {
               this.x=x;
               this.y=y;
               this.x1=x1;
               this.y1=y1;
        }
        
        public void showWidth() {
               System.out.println("矩形的宽是 : "+Math.abs((x1-x)));
         }
         public void showHeidht() {
               System.out.println("巨型的高是 : "+Math.abs((y1-y)));
          }
         public void showArea() {
               System.out.println("矩形的面积是 : "+Math.abs((x1-x)*(y1-y)));
         }
    }
      

  5.   

    int getHeight()//计算高heigth 

    height=y2=y1;  // 问题!!!
    return height; 
      

  6.   

    details is the key to success!
      

  7.   

    你要先写2个set方法给width,height赋值.
    get方法是取值的,没有用set方法给width,height这2个属性设置值,你的getArea方法当然不能计算出值了.
      

  8.   


    import java.util.Scanner;
    import java.io.*;
    public class Bidu{

    int x1,x2,y1,y2;
    int width,heigh,area;
    public void setBidu(int x1,int x2,int y1,int y2){
    this.x1=x1;
    this.x2=x2;
    this.y1=y1;
    this.y2=y2;
    }
    public int getWidth(){
    width=x2-x1;
    return width;
    }
    public int getHeigh(){
    heigh=y2-y1;
    return heigh;
    }

    public int getArea(){
    area=width*heigh;
    return area;
    }

    public static void main(String args []){

    Bidu bidu=new Bidu();
    //String input="12 23 45 56";
    int count=0;

    outer:while(true){
    count++;
    System.out.println("--------------------------------------");
    System.out.print("第"+count+"次"+"请输入四个坐标如(x1 x2 x3 x4)示:");
    Scanner s=new Scanner(System.in);//.useDelimiter("\\s* \\s*");//使用正则表达式来取数;
    int a1=s.nextInt();
    int a2=s.nextInt();
    int a3=s.nextInt();
    int a4=s.nextInt();
    if(a1<0||a2<0||a3<0||a4<0){
    System.out.println("你输入的数非法,请重新输入");
    count=0;
    continue outer;
    }
    else if(a1>a2){
    int temp=a1;
    a1=a2;
    a2=temp;
    }
     if(a3>a4){
    int temp=a3;
    a3=a4;
    a4=temp;
    }
    bidu.setBidu(a1,a2,a3,a4);

    System.out.println("矩形宽为"+bidu.getWidth());
    System.out.println("矩形长为"+bidu.getHeigh());
    System.out.println("矩形的面积为"+bidu.getArea());
    if(count==4)break;
    }
    }
    }
      

  9.   

    计算面积的算法也有问题的说,变量应该用坐标点X1、X2、Y1、Y2,否则就有可能出现结果为0的BUG