Java - 两个圆圈重叠或在里面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22032342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Two Circles overlap or inside
提问by MalPal
I have a problem on my homework, and I am having trouble getting the last part to work. What am I doing wrong here? It will not print "Circle 2 overlaps Circle 1." or "Circle 2 does not overlap Circle 1." The first two cases do print. Here is the question:
我的作业有问题,我无法完成最后一部分。我在这里做错了什么?它不会打印“圆 2 与圆 1 重叠”。或“圆 2 不与圆 1 重叠。” 前两种情况确实打印。这是问题:
Write a program (TwoCircles.java) that prompts the user to enter the center coordinates and radii of two circles and determines the geometrical relationship between the two and print one of the following messages accordingly: 1. Circle 1 is inside Circle 2. 2. Circle 2 is inside Circle 1. 3. Circle 2 overlaps Circle 1. 4. Circle 2 does not overlap Circle 1.
编写一个程序 (TwoCircles.java),提示用户输入两个圆的中心坐标和半径,并确定两者之间的几何关系,并相应地打印以下消息之一: 1. 圆 1 在圆 2 内。 2.圆 2 在圆 1 内。 3. 圆 2 与圆 1 重叠。 4. 圆 2 不与圆 1 重叠。
Here is what i have so far for my code:
到目前为止,这是我的代码:
import java.util.Scanner;
public class TwoCircles {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Circle 1 center x-, y-coordinates, and radius: ");
double X1 = input.nextDouble();
double Y1 = input.nextDouble();
double radius1 = input.nextDouble();
System.out.print("Enter Circle 2 center x-, y-coordinates, and radius: ");
double X2 = input.nextDouble();
double Y2 = input.nextDouble();
double radius2 = input.nextDouble();
double distance = Math.pow((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2), 0.5);
if (radius2 >= radius1){
if (distance <= (radius2 - radius1))
System.out.println("Circle 1 is inside Circle 2.");}
else if (radius1 >= radius2){
if (distance <= (radius1 - radius2))
System.out.println("Circle 2 is inside Circle 1.");}
else if (distance > (radius1 + radius2)){
System.out.println("Circle 2 does not overlap Circle 1.");}
else {
System.out.println("Circle 2 overlaps Circle 1.");}
}
}
Any guidance appreciated
任何指导表示赞赏
回答by Bhoot
Use the following code:
使用以下代码:
import java.util.Scanner;
public class TwoCircles {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Circle 1 center x-, y-coordinates, and radius: ");
double X1 = input.nextDouble();
double Y1 = input.nextDouble();
double radius1 = input.nextDouble();
System.out.print("Enter Circle 2 center x-, y-coordinates, and radius: ");
double X2 = input.nextDouble();
double Y2 = input.nextDouble();
double radius2 = input.nextDouble();
double distance = Math.pow((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2), 0.5);
if (radius2 >= radius1 && distance <= (radius2 - radius1)){
System.out.println("Circle 1 is inside Circle 2.");
}
else if (radius1 >= radius2 && distance <= (radius1 - radius2) ) {
System.out.println("Circle 2 is inside Circle 1.");
}
else if (distance > (radius1 + radius2)){
System.out.println("Circle 2 does not overlap Circle 1.");
}
else {
System.out.println("Circle 2 overlaps Circle 1.");}
}
}
The problem with the code which you are using is that either of the first two conditions:
您使用的代码的问题是前两个条件之一:
if(radius1>=radius2)
or
或者
else if(radius1<=radius2)
will always be true and hence the code will never reach the third or fourth condition. The correct way to achieve what you are trying to do is to merge all the conditions together which i have done in the code presented above. Hope this helps!
将永远为真,因此代码永远不会达到第三或第四个条件。实现您想要做的事情的正确方法是将我在上面提供的代码中所做的所有条件合并在一起。希望这可以帮助!
回答by libik
You probably "lost" yourself in that if-else statements.
您可能在 if-else 语句中“迷失”了自己。
First of all, I would recommend to write it with ALL the braces to understand correctly, what exactly is happening there :
首先,我建议用所有大括号编写它以正确理解那里到底发生了什么:
if (radius2 >= radius1) {
if (distance <= (radius2 - radius1)) {
System.out.println("Circle 1 is inside Circle 2.");
}
} else if (radius1 >= radius2) {
if (distance <= (radius1 - radius2)) {
System.out.println("Circle 2 is inside Circle 1.");
}
} else if (distance > (radius1 + radius2)) {
System.out.println("Circle 2 does not overlap Circle 1.");
} else {
System.out.println("Circle 2 overlaps Circle 1.");
}
As you can see, you say "if radius2 is equal or greater than radius1" do something and if not and if "radius1 is greater or equal radius2" do something else.
如您所见,您说“如果radius2 等于或大于radius1”,则执行某些操作,否则,如果“radius1 大于或等于radius2”,则执行其他操作。
If you think about it, there is no possibility which is neither first or second condition. So the last two statements are logically unreachable.
如果你考虑一下,没有任何可能性既不是第一条件也不是第二条件。所以最后两个语句在逻辑上是不可达的。