Java黑皮书课后题第3章:**3.29(几何:两个圆)编写程序,提示用户输入两个圆的中心坐标和各自的半径值,然后判断圆是在第一个圆内,还是和第一个圆重叠
**3.29(幾何:兩個(gè)圓)編寫程序,提示用戶輸入兩個(gè)圓的中心坐標(biāo)和各自的半徑值,然后判斷圓是在第一個(gè)圓內(nèi),還是和第一個(gè)圓重疊
- 題目
- 題目概述
- 運(yùn)行示例
- 代碼
題目
題目概述
**3.29(幾何:兩個(gè)圓)編寫程序,提示用戶輸入兩個(gè)圓的中心坐標(biāo)和各自的半徑值,然后判斷圓是在第一個(gè)圓內(nèi),還是和第一個(gè)圓重疊
原書自帶提示:如果兩個(gè)圓心的距離≤|r1-r2|,可以判斷circle2在circle1內(nèi);如果兩個(gè)圓心的距離≤r1+r2,可以判斷circle2和circle1重疊
運(yùn)行示例
Enter circle1’s center x-, y-coordinates, and radius: 0.5 5.1 13
Enter circle2’s center x-, y-coordinates, and radius: 1 1.7 4.5
circle2 is inside circle1
Enter circle1’s center x-, y-coordinates, and radius: 3.4 5.7 5.5
Enter circle2’s center x-, y-coordinates, and radius: 6.7 3.5 3
circle2 overlaps circle1
Enter circle1’s center x-, y-coordinates, and radius: 3.4 5.5 1
Enter circle2’s center x-, y-coordinates, and radius: 5.5 7.2 1
circle2 does not onverlap circle1
代碼
import java.util.Scanner;public class Test3_29 {public static void main(String[] args) {// 接收用戶輸入Scanner input = new Scanner(System.in);System.out.println("Enter circle1's center x-, y-coordinates, and radius: ");double x1 = input.nextDouble(), y1 = input.nextDouble(), r1 = input.nextDouble();System.out.println("Enter circle2's center x-, y-coordinates, and radius: ");double x2 = input.nextDouble(), y2 = input.nextDouble(), r2 = input.nextDouble();// 計(jì)算兩個(gè)圓心的距離double distance = Math.pow(Math.pow(x1-x2,2) + Math.pow(y1-y2,2), 0.5);// 判斷距離與圓心之間的關(guān)系if(distance < Math.abs(r1-r2))System.out.println("circle2 is inside circle1");else if(distance < r1+r2)System.out.println("circle2 overlaps circle1");elseSystem.out.println("circle2 does not onverlap circle1");} }總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第3章:**3.29(几何:两个圆)编写程序,提示用户输入两个圆的中心坐标和各自的半径值,然后判断圆是在第一个圆内,还是和第一个圆重叠的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第3章:**3.28
- 下一篇: Java黑皮书课后题第3章:*3.30(