简体   繁体   English

为什么我的变量不使用输入值

[英]Why aren't my variables using the input value

For a project I have to use a constructor to set default values for a rectangle height and width and also be able to input numbers.对于一个项目,我必须使用构造函数来设置矩形高度和宽度的默认值,并且还能够输入数字。 however when I input the program continues to use default values any way to get it to only use default values when there is no input?但是,当我输入时,程序继续使用默认值以任何方式使其仅在没有输入时使用默认值?

package geometry;
// file name Rectangle.java
/**
 *
 * @author 12484
 */
public class Rectangle {

 // **********************instance variables (properties)
 private double myWidth;
 private double myLength;
// ********************************* constructors
 // default constructor

 public Rectangle() {
  myWidth = 1;
  myLength = 1;
 }
 // "other" constructor

 public Rectangle (double width, double length) {
  myWidth = width;
  myLength = length;
 }

 // ********************************* accessor methods
 public double getWidth() {
  return myWidth;
 }

 public double getLength() {
  return myLength;
 }

 // ********************************* modifier methods
 public void setWidth(double width) {
  myWidth = width;
 }

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

 // ********************************* interesting methods
 public double computeArea() {
  getLength();
  getWidth();
  double area = Math.pow(getLength(), getWidth());
  return area;
 }

 public double computePerimeter() {
  getLength();
  getWidth();
  double perimeter = Double.sum(getLength(), getWidth());
  return perimeter;
 }
}


package geometry;
// filename Rectest.java
public class Rectest {
    public static void main(String[]args){
        Rectangle len = new Rectangle();
        Rectangle wid = new Rectangle();
        Rectangle a = new Rectangle();
        len.setLength(5.2);
        wid.setWidth(7.5);
        System.out.println(a.computeArea());
    }
}

default values are 1 for length and 1 for width默认值是 1 的长度和 1 的宽度

len,wid and a are all different objects(rectangles in this case) with their own instance variables(width and length). len、wid 和 a 都是不同的对象(在这种情况下为矩形),具有自己的实例变量(宽度和长度)。

What you want to do is create a Rectangle object, set its length and width and then execute the computeArea on that object.您要做的是创建一个矩形 object,设置其长度和宽度,然后在该 object 上执行 computeArea。

public class Rectest {
    public static void main(String[]args){
        Rectangle a = new Rectangle();
        a.setLength(5.2);
        a.setWidth(7.5);
        System.out.println(a.computeArea());
    }
}

What you actually want to do is the following:您真正想要做的是以下内容:

public class Rectest {
    public static void main(String[]args){
        Rectangle rectangle = new Rectangle();
        rectangle.setLength(5.2);
        rectangle.setWidth(7.5);
        System.out.println(rectangle.computeArea());
    }
}

Or even better since you have a constructor for both length and width :甚至更好,因为您有一个lengthwidth的构造函数:

public class Rectest {
    public static void main(String[]args){
        Rectangle rectangle = new Rectangle(7.5, 5.2);
        System.out.println(rectangle.computeArea());
    }
}

In your example you were setting length in one of the Rectangle objects, then the width in another and finally, computing the are of a third one that was untouched and thus with the default values for both length and width .在您的示例中,您在其中一个Rectangle对象中设置length ,然后在另一个中设置width ,最后,计算未触及的第三个对象的区域,因此使用lengthwidth的默认值。

Finally, as already mentioned by @harold and @JimRhodes (thanks to you both), your computeArea() and computePerimeter() methods are wrong.最后,正如@harold 和@JimRhodes 已经提到的(感谢你们俩),您的computeArea()computePerimeter()方法是错误的。 They should be:他们应该是:

public double computeArea() {
  return myLength * myWidth;
}

public double computePerimeter() {
  return 2 * (myLength + myWidth);
}

The calls to getLength() and getWidth() had no meaning or effect and thus are not needed.getLength()getWidth()的调用没有任何意义或效果,因此不需要。

Because when you are create Rectangle a.因为当您创建 Rectangle a. you are using the default constructor.您正在使用默认构造函数。

Rectangle a = New Rectangle();矩形 a = 新矩形(); it should be Rectangle a = New Rectangle(7.5,5.2)它应该是 Rectangle a = New Rectangle(7.5,5.2)

assuming that you are trying to make a rectangle 7.5 by 5.2.假设您正在尝试制作一个 7.5 x 5.2 的矩形。

Your code is actually making three rectangles.您的代码实际上是在制作三个矩形。 I have commented your code below to help you better understand what each line is doing.我在下面评论了您的代码,以帮助您更好地了解每一行的作用。

//This creates a new rectangle using the default constructor        
Rectangle len = new Rectangle();

//This creates a new rectangle using the default constructor        
Rectangle wid = new Rectangle();

//This creates a new rectangle using the default constructor
Rectangle a = new Rectangle();
//This sets Rectangle len length to 5.2
len.setLength(5.2);

//this set Rectangle wid width to 7.5
wid.setWidth(7.5);
//This computes the area of rectangle a.
System.out.println(a.computeArea());

to better understand try adding this to your code.为了更好地理解尝试将其添加到您的代码中。 System.out.println(len.computeArea()); System.out.println(len.computeArea()); // 5.2 System.out.println(wid.computeArea()); // 5.2 System.out.println(wid.computeArea()); //7.5 //7.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM