【JAVA 第四章 流程控制语句】课后习题 二维坐标距离 三角形判断等

知识点:
1.三角形的判断公式 两边之和大于第三边
2.二维坐标两点距离公式 根号[(x1-x2)平方+(y1-y2)平方]
3.JAVA Math.pow(变量,指数); 的运用
4.JAVA Math.sqrt(); 求平方根的运用

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        System.out.println("选择输入 选择要使用的功能:");
        System.out.println("1.计算坐标到(0,0)的距离");
        System.out.println("2.计算两个坐标之间的距离");
        System.out.println("3.判断三个坐标是否构成三角形");
        //键入选择
        int i =0;
        int x1;
        int y1;

        int x2;
        int y2;

        int x3;
        int y3;

        //实例化对象
        Point p = new Point();
        boolean k = true;
        while (k){
            try {
                Scanner scan = new Scanner(System.in);
                i = scan.nextInt();
                if (i   < 0 || i > 3){
                    System.out.println("选择错误,请正确选择!");
                }else {

                if (i == 1){
                    System.out.println("# 1 #");
                    System.out.println("请输入坐标X1:");
                    x1 =scan.nextInt();
                    System.out.println("请输入坐标Y1:");
                    y1 =scan.nextInt();
                    p.distance(x1,y1);
                }else if (i == 2){
                    System.out.println("# 2 #");
                    System.out.println("请输入坐标X1:");
                    x1 =scan.nextInt();
                    System.out.println("请输入坐标Y1:");
                    y1 =scan.nextInt();

                    System.out.println("请输入坐标X2:");
                    x2 =scan.nextInt();
                    System.out.println("请输入坐标Y2:");
                    y2 =scan.nextInt();
                    p.distanceOfPoint(x1,y1,x2,y2);
                }else {
                    System.out.println("# 3 #");
                    System.out.println("请输入坐标X1:");
                    x1 =scan.nextInt();
                    System.out.println("请输入坐标Y1:");
                    y1 =scan.nextInt();

                    System.out.println("请输入坐标X2:");
                    x2 =scan.nextInt();
                    System.out.println("请输入坐标Y2:");
                    y2 =scan.nextInt();

                    System.out.println("请输入坐标X3:");
                    x3 =scan.nextInt();
                    System.out.println("请输入坐标Y3:");
                    y3 =scan.nextInt();

                    p.traingle(x1,y1,x2,y2,x3,y3);
                }

                    k = false;}

            } catch (Exception e) {
                System.out.println("输入有误,请重新输入!");

            }
        }




    }
}

class Point{

    public int x,y;//横纵坐标

    //无参构造方法
    public Point(){
        this(0,0);
    }
    //带有两个参数的构造方法
    public Point(int x,int y){
        this.x = x;
        this.y = y;
    }
    //移动坐标
    public void moveTo(int x,int y){
        this.x = x;
        this.y = y;
    }
    //计算给定点到0,0坐标的距离
    //二维坐标两点之间的距离计算公式 #根号 [(x1-x2)²+(y1-y2)²]#
    public void distance(int x,int y){
        double distance=0.0;

        distance = Math.sqrt(      Math.pow(x-0,2)      +     Math.pow(y-0,2)    );

        System.out.println("您输入的坐标:\t"+"("+x+","+y+")");
        System.out.println();
        System.out.println("它到 (0,0)之间的距离是:\t"+distance);

        return;
    }

    //计算给点两个点之间的距离
    //二维坐标两点之间的距离计算公式 #根号 [(x1-x2)²+(y1-y2)²]#
    public void distanceOfPoint(int x1,int y1,int x2,int y2){
        double distanceOfPoint = 0.0;

        distanceOfPoint = Math.sqrt(  Math.pow(x1-x2,2) +   Math.pow(y1-y2,2)  );


        System.out.println("您输入的坐标:\t"+"("+x1+","+y1+")"+"\t"+"("+x2+","+y2+")");
        System.out.println();
        System.out.println("它们之间的距离是:\t"+distanceOfPoint);

        return;

    }

    //判断三个点是否能构成一个三角形
    public void traingle(int x1,int y1,int x2,int y2,int x3,int y3){
        double dis1 = 0.0;
        double dis2 = 0.0;
        double dis3 = 0.0;
        //计算各点之间距离 既是边长

        dis1 = Math.sqrt(   Math.pow(x1-x2,2) + Math.pow(y1-y2,2)   );


        dis2 = Math.sqrt(   Math.pow(x1-x3,2) + Math.pow(y1-y3,2)   );


        dis3 = Math.sqrt(   Math.pow(x2-x3,2) + Math.pow(y2-y3,2)   );
        //两边之和大于第三边判断是否构成三角形
        if (dis1 + dis2 > dis3 && dis1 + dis3 > dis2 && dis2 + dis3 > dis1){
            System.out.println("您输入的坐标:\t"+"("+x1+","+y1+")"+"("+x2+","+y2+")"+"("+x3+","+y3+")"+"\t构成一个三角形");
        }else{
            System.out.println("您输入的坐标:\t"+"("+x1+","+y1+")"+"("+x2+","+y2+")"+"("+x3+","+y3+")"+"\t不构成一个三角形");
        }
        return;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值