-4

有人可以告诉我这个简单的程序有什么问题吗?我的输出为“0”。

package myConst;

public class Doconstructor
{
    int length,width;

    Doconstructor(int x, int y)
    {
        int area;
        area = length * width;
        System.out.println("area ="+area);
    }
}

class work
{
    public static void main(String args[])
    {
        Doconstructor d1 = new Doconstructor(10, 15);
    }
}
4

9 回答 9

1
Doconstructor d1 = new Doconstructor(10, 15);
// you are assigning values for x and y

Doconstructor (int x,int y)
{ 
    int area;            // you are never use x and y values for calculation
    area = length *width; // so area remain 0 since current length and width is 0
    System.out.println("area ="+area);
}

您需要按如下方式更改代码。

Doconstructor (int x,int y)
{
    int area;
    this.length=x;
    this.width=y;
    area = length *width;
    System.out.println("area ="+area);
}
于 2013-11-06T04:50:01.250 回答
1

像这样编辑:-

 package myConst;

    public class Doconstructor
    {
        int length,width;

        Doconstructor(int x, int y)
        {
            int area;
    this.length=x;//Using this for current object
    this.width=y;//Using this for current object
            area = length * width;
            System.out.println("area ="+area);
        }
    }

    class work
    {
        public static void main(String args[])
        {
            Doconstructor d1 = new Doconstructor(10, 15);
        }
    }

您的输出将是:

面积=150

必须阅读:

http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

于 2013-11-06T06:28:26.233 回答
0

You have this:

public class Doconstructor {

    int length,width;

    Doconstructor (int x,int y)
    {
        int area;
        area = length *width;
        System.out.println("area ="+area);
    }
}

At no point do you set length or width equal to anything. Their initial values are 0 and your program is doing precisely what you told it to do. area = length * width = 0 * 0 = 0.

You also are not doing anything with the x or y that you passed to the constructor, but this probably was not your intention. When writing programs, you basically need to clearly instruct the computer to do what you want to do. It's not going to guess what you want. If you ignore x and y, and don't assign any values to length or width, then that is precisely what will happen and you cannot be surprised when you see the results you see.

于 2013-11-06T04:36:33.530 回答
0

you are writing int length,width at class level so length and width are set to 0 as default. After that in the constructor you are not setting any values to length and width so you are the values for length and width are 0.Hence area is also 0 Please check this link for list of default values

于 2013-11-06T04:36:51.330 回答
0

Constructors are used to create objects and to set the attributes. You are not setting the attributes in your constructor. Here is how your constructor should look like.

Doconstructor(int x, int y){
   length = x;
   width = y;
}

Secondly you are mixing the logic of a constructor and a method. You are doing the calculation of area, which seems to be a perfect fit for another method in your class. so better move that logic in a separate method:

public int calculateArea() {
   int area;
   area = x * y;
   return area;
}

Finally create an object using constructor to set the attributes length and width. And then call calculateArea method to do the business logic of calculating area.

public static void main(String args[]){
    Doconstructor d1 = new Doconstructor(10, 15); // create object and set length & width
     d1.calculateArea();
}
于 2013-11-06T04:38:01.287 回答
0

您没有设置lengthand的值,width默认情况下它们都是0. 您可能必须这样做:

Doconstructor(int x, int y){
   int area;
   area = x * y;
   length = x;
   width = y;
   System.out.println("Area = "+area);
}
于 2013-11-06T04:33:58.453 回答
0

您没有使用传递给构造函数的变量值,而是使用已初始化为 0 的长度和宽度值。您需要area = x * y;

于 2013-11-06T04:34:32.837 回答
0

长度和宽度字段被隐式初始化为 0。将它们相乘得到 0。

我想你想要的是

length = y ;
width = x ;
int area = length * width ;
System.out.println("area ="+area);
于 2013-11-06T04:35:25.557 回答
0

您没有将 x 和 y 的值分配给变量宽度和长度。宽度和长度的默认值是 (int) 0。这就是你得到输出 (0*0=0) 的原因。首先将值分配给变量或使用“area=x*y;” .

于 2013-11-06T04:42:14.020 回答