第三章第二十二题(几何:点是否在圆内?)(Geometry: point in a circle?)

    科技2022-08-06  113

    第三章第二十二题(几何:点是否在圆内?)(Geometry: point in a circle?)

    **3.22(几何:点是否在圆内?)编写程序,提示用户输入一个点(x, y),然后检查这个点是否在以原点(0,0)为圆心、半径为10的圆内。例如:(4,5)是圆内的一点,而(9,9)是圆外的一点。 下面是运行示例: Enter a point with two coordinates: 4 5 Point (4.0, 5.0) is in the circle Enter a point with two coordinates: 9 9 Point (9.0, 9.0) is not in the circle

    **3.22(Geometry: point in a circle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the circle centered at (0, 0) with radius 10. For example, (4, 5) is inside the circle and (9, 9) is outside the circle. Here are some simple runs: Enter a point with two coordinates: 4 5 Point (4.0, 5.0) is in the circle Enter a point with two coordinates: 9 9 Point (9.0, 9.0) is not in the circle

    参考代码:

    package chapter03; import java.util.Scanner; public class Code_22 { public static void main(String[] args) { double pointX,pointY,distance; System.out.print("Enter a point with two coordinates:"); Scanner input = new Scanner(System.in); pointX = input.nextDouble(); pointY = input.nextDouble(); // Calculate the distance from (0,0) to (pointX,pointY) distance = Math.pow(Math.pow(pointX, 2) + Math.pow(pointY, 2), 0.5); // Check if point is in the circle if(distance <= 10) System.out.println("Point " + "(" + pointX + "," + pointY + ")" + " is in the circle"); else System.out.println("Point " + "(" + pointX + "," + pointY + ")" + " is not in the circle"); input.close(); } } 结果显示: Enter a point with two coordinates:4 5 Point (4.0,5.0) is in the circle Process finished with exit code 0
    Processed: 0.015, SQL: 9