Home > Java > Javagetting Started > body text

Use java to solve triangle angle problems

王林
Release: 2021-02-04 10:05:58
forward
2263 people have browsed it

Use java to solve triangle angle problems

Triangle angle formula:

Use java to solve triangle angle problems

From the above formula we can see that to calculate the angle, we must know the side length, So how do we calculate the side length?

First we need to know the coordinates of the three vertices, and then calculate the distance between points.

Code:

package com.zhuo.base.com.zhuo.base;

import java.util.Scanner;

public class ComputeAngles {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        //提示用户输入三个点
        System.out.print("Enter three points:");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        double x3 = input.nextDouble();
        double y3 = input.nextDouble();
        //计算三条边
        double a = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2- y3));
        double b = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
        double c = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        //计算三个角
        double A = Math.toDegrees(Math.acos((a * a - b * b - c * c) / (-2 * b * c)));
        double B = Math.toDegrees(Math.acos((b * b - a * a - c * c) / (-2 * a * c)));
        double C = Math.toDegrees(Math.acos((c * c - a * a - b * b) / (-2 * a * b)));
        //显示结果,保留小数点后两位
        System.out.println("The three angles are " +
                Math.round(A * 100) / 100.0 + " " +
                Math.round(B * 100) / 100.0 + " " +
                Math.round(C * 100) / 100.0);
    }
}
Copy after login

Result display:

Use java to solve triangle angle problems

Related recommendations: java introductory tutorial

The above is the detailed content of Use java to solve triangle angle problems. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template