【JAVA】程序判断用户输入的点跟圆的位置关系

问题: 简单编写一个程序,提示用户输入一个点(x,y),并检查该点是否在半径为10的圆圈(0,0)内。例如,(4,5)在圆内,(9,9)在圆外。

package Experiment;
import java.util.Scanner;
public class Geometry {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        final double DISTANCE = 10.0;
        // input a point and judge
        System.out.print("Enter a point with two coordinates:");
        // input x
        double x=input.nextDouble();
        // input y
        double y=input.nextDouble();
        double dist = Math.sqrt(x*x+y*y);
        if(dist>DISTANCE)
        System.out.printf("Point(%4.1f, %4.1f) is outside the circle.\n", x, y);
        else if(dist==DISTANCE)
        System.out.printf("Point(%4.1f, %4.1f) is on the circle.\n", x, y);
        else
        System.out.printf("Point(%4.1f, %4.1f) is in the circle.\n", x, y);
        input.close();
    }
}

运行结果:

圆内和圆外:

圆上:

如有问题和改进,期待大家指出,谢谢啦~

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值