如何检查矩形是否适合放入另一个矩形内?

huangapple go评论127阅读模式
英文:

How to check if Rectangle will fit inside another rectangle?

问题

我正在尝试编写一个方法该方法接受两个矩形参数如果第一个矩形适应于第二个矩形内部则返回true否则返回false

import java.awt.Rectangle;

public class Square {
    public static void main(String[] args) {
        Rectangle rect1 = new Rectangle(0, 0, 100, 200);
        Rectangle rect2 = new Rectangle(0, 0, 100, 200);
        fitsInside(rect1, rect2);
    }

    public static boolean fitsInside(Rectangle rec1, Rectangle rec2) {
        if (rec1.width < rec2.width && rec1.height < rec2.height) {
            return true;
        } else {
            return false;
        }
    }
}
当我编译和运行这段代码时它没有返回任何结果为什么会出现这种情况我该如何修复
英文:

I am trying to write a method that takes two rectangle parameters, and will return true if the first rectangle fits inside the second rectangle and false if it doesn't.

import java.awt.Rectangle;
public class Square {

    public static void main (String[] args) {
        Rectangle rect1 = new Rectangle(0,0,100,200);
        Rectangle rect2 = new Rectangle(0,0,100,200);
        fitsInside(rect1, rect2);
    }

    public static boolean fitsInside(Rectangle rec1, Rectangle rec2) {

        if (rec1.width &lt; rec2.width &amp;&amp; rec1.height &lt; rec2.height) {
            return true;
        } else {
            return false;
        }
    }
}

When I compile and run this code it returns nothing. Why does this not work and how I could fix it?

答案1

得分: 3

你的方法是正确的。问题是你没有输出你的答案。
将调用 fitsInside 的那行代码用 System.out.println(...) 包围起来,这样它会显示出你正在计算的结果。

英文:

Your approach is right. The thing is you're not outputting your answer.

Surround the line where you call fitsInside with System.out.println(...) and it will display the result you're computing.

答案2

得分: 1

函数fitsInside返回一个布尔变量,但是您没有使用它的返回值。由于您没有打印任何内容,您没有得到结果。

打印结果将会解决您的问题:

System.out.println(fitsInside(rect1, rect2));
英文:

The function fitsInside returns a boolean variable, but you are not using its return value. Since you haven't printed anything, you didn't get the result.

Printing the result will solve your problem:

System.out.println(fitsInside(rect1, rect2));

huangapple
  • 本文由 发表于 2020年10月9日 00:07:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64266452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定