0

我是 stackoverflow 的新手,我正在为我的一类(特别是 CSC 202J)开发一个项目,我几乎完成了,但我遇到了一个小问题 - 输入功能无法正常工作。我已经对此进行了编程,以便矩形的默认宽度和长度为 1.0,但是当我调用任何一个 get 方法时,我发现这两个值都没有改变。我将提供我的代码(连同驱动程序类和分配要求,以及测试运行的输出。

任务:

创建一个矩形类。该类具有长度和宽度属性,每个属性默认为 1。提供计算矩形周长和面积的方法。
提供长度和宽度的设置和获取方法。set 方法应验证 length 和 width 都是大于或等于 0.0 且小于 20.0 的浮点数。
编写一个程序来测试类 Rectangle。

样品输出:

1. Set Length
2. Set Width
3. Exit
Choice: 1
Enter Length: 10
Length: 10.00
Width: 1.00
Perimeter: 22.00
Area: 10.00
1. Set Length
2. Set Width
3. Exit
Choice: 2
Enter Width: 15
Length: 10.00
Width: 15.00
Perimeter: 50.00
Area: 150.00
1. Set Length
2. Set Width
3. Exit
Choice: 1
Enter Length: 1
Length: 1.00
Width: 15.00
Perimeter: 32.00
Area: 15.00
1. Set Length
2. Set Width
3. Exit
Choice: 3

课程:

import java.util.Scanner;

public class RectangleTest {
public static void main (String[] args)
{
   int response = 0;
   Scanner userInput = new Scanner(System.in);
   Rectangle r = new Rectangle();

     do{

      System.out.println("1. Set Length");
      System.out.println("2. Set Width");
      System.out.println("3. Exit");
      System.out.println("Choice: ");
      response = userInput.nextInt();

        if(response == 1)
        {
            System.out.println("Enter Length: ");
            r.setLength(userInput.nextInt());
            r.toString();
            System.out.println("Length: " + r.getLength());
            System.out.println("Width: " + r.getWidth());
            System.out.println("Perimeter: " + r.perimeter());
            System.out.println("Area: " + r.area());
        }
        if(response == 2)
        {
            System.out.println("Enter Width: ");
            r.setWidth(userInput.nextInt());
            r.toString();
            System.out.println("Length: " + r.getLength());
            System.out.println("Width: " + r.getWidth());
            System.out.println("Perimeter: " + r.perimeter());
            System.out.println("Area: " + r.area()); 
        }
      }
    while(response != 3);
    System.exit(0);
  }

}



public class Rectangle {
    private double length;
    private double width;

public Rectangle(){
    this.length = 1.0;
    this.width = 1.0;
  }

public double getLength(){
    return this.length;
  }

public double getWidth(){
   return this.width;
  }

public boolean setLength(double length){
   if (length > 0.0 && 1 < 20.0){
      return true;
   }
    return false;
 }

public boolean setWidth(double width){
    if(width > 0.0 && width < 20.0){
       return true;
   }
    return false;
}

public double perimeter(){
   return 2 * (this.length + this.width);
}

public double area(){
   return this.getLength() * this.getWidth();
}

@Override
public String toString(){
   return "Length: " + this.length +"\tWidth: " + this.width;
}

}

测试运行输出:

1. Set Length
2. Set Width
3. Exit
Choice: 
1
Enter Length: 
10
Length: 1.0
Width: 1.0
Perimeter: 4.0
Area: 1.0
1. Set Length
2. Set Width
3. Exit
Choice: 
2
Enter Width: 
14
Length: 1.0
Width: 1.0
Perimeter: 4.0
Area: 1.0
1. Set Length
2. Set Width
3. Exit
Choice: 
3

如果这是我忽略的新手错误,我深表歉意。

4

2 回答 2

0

You never set the value in your setXXX() methods. Include this.width = width and this.length = length in your code.

public class Rectangle {
    private double width;
    private double length;

    public boolean setWidth(double width) {
        if(width >= 0.0 && width < 20.0) {
            this.width = width; // <<< You need this line ...
            return true;
        } else return false;
    }

    public boolean setLength(double length) {
        if(length >= 0.0 && length < 20.0) {
            this.length = length; // <<< ... and this one, too!
            return true;
        } else return false;
    }
}
于 2016-02-18T20:15:51.460 回答
-1

为了更改矩形类的值,您必须执行以下操作:

public void setwidth(double width){
    if (width => 0.0 && width < 20.0){
        this.width = width;
    }
}

这同样适用于长度。目前,您的代码返回一个布尔值,指示输入宽度是否在特定值内。set 方法不需要返回任何内容。

我在 setLength 方法中注意到您的代码中的另一个错误;

public boolean setLength(double length){
   if (length > 0.0 && 1 < 20.0){
      return true;
   }
    return false;
   }

您是否打算length在它所说的地方输入 if 语句if (length> 0.0 && 1 < 20.0)

另一个注意事项:在您的分配描述中,声明宽度和长度需要“大于或等于零”,因此在代码中 if 语句看起来像if (width >= 0.0 && width < 20.0). 注意大于或等于符号。

于 2016-02-18T20:31:28.687 回答