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

第三章第二十二题(几何:点是否在圆内?)(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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值