gpt4 book ai didi

Java 类将大于视为大于或等于

转载 作者:行者123 更新时间:2023-12-02 02:10:04 27 4
gpt4 key购买 nike

我有一个 Java 作业,要向程序中添加 throw/catch。一切正常,除了它应该捕获任何小于 0 或大于 20 的值,它会捕获 -1 及以下或 21 及以上,但它似乎将大于视为大于或等于

类(class):

public class Rectangle {

private double width;
private double height;

public Rectangle(double width, double height) {
setWidth(width);
setHeight(height);
}

public void setWidth(double width) {
if (width < 0 || width > 20) {
throw new IllegalArgumentException("Width is not in range");
}
this.width = width;
}

public void setHeight(double height) {
if (height < 0 || height > 20) {
throw new IllegalArgumentException("Height is not in range");
}
this.height = height;
}

public double getWidth() {
return width;
}

public double getHeight() {
return height;
}

public double calculatePerimeter() {
return 2 * (width + height);
}

public double calculateArea() {
return width * height;
}
}

测试类:

public class RectangleTest {

public static void main(String[] args) {

try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(5, 5);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(25, 5);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(-5, 5);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(0, 5);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(5, 25);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(5, 0);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(5, -5);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(20,0);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}

为什么它将 0 和 20 视为有效值?

最佳答案

根据您的代码:

if(width < 0 || width > 20)

表示如果宽度= 0:

width < 0 = false 
width > 20 = false

if(false || false ) => false 因为它是 OR 运算符。

如果您想检查 0 到 20 之间的值(含 0 和 20)。

您的条件应为(宽度 <= 0 ||宽度 >= 20)

如果您想专门检查 0 到 20 之间的值。

您的条件应为(宽度 < 0 || 宽度 > 20)

关于Java 类将大于视为大于或等于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50146253/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com