问题: 简单编写一个程序,提示用户输入一个点(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();
}
}
运行结果:
圆内和圆外:
圆上: