编写程序,提示用户输入一个点(x, y),然后检查这个点是否在以原点(0, 0)为圆心、半径为10的圆内。
提示:
如果一个点到(0,0)的距离小于或等于10,那么该点就在圆内,计算距离的公式是:
√((x2 - x1)^2 + (y2 - y1)^2)
package pack2;
import java.util.Scanner;
public class InCircle {
public static void main(String[] args) {
try(Scanner input = new Scanner(System.in);) {
System.out.print("Enter a point with two coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();
System.out.printf("Point (%.1f, %.1f) is "+(isInCircle(x, y) ? "" : "not ")+
"in the circle", x, y);
}
}
//判定是否在圆中
public static boolean isInCircle(double x, double y) {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) <= 10;
}
}