Java- 在公式方面需要帮助

标签 java math trigonometry jcreator

我最近在这里发帖,但我再次需要帮助(我是新手)我得到了第一部分 (SSS) 但它是第二部分我需要帮助,我不明白如何把 a² = b² + c² - 2bc cosA in 和 sin B/b = sin A/a 这是我的代码:

import java.util.Scanner;

public class CosineLaw {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    boolean sss = new Boolean(true);

    System.out.println("Are you working with an SSS?[y/n]");
    char askingSSS =keyboard.next().charAt(0);  
    if(askingSSS == 'y'){
        System.out.println("Please enter an a side value:");
        double a = keyboard.nextDouble();
        System.out.println("Please enter a b side value:"); 
        double b = keyboard.nextDouble();
        System.out.println("Please enter a c side value:");
        double c = keyboard.nextDouble();
            double answerA = Math.toDegrees(Math.acos((b*b+c*c-a*a) / (2*b*c)));
            double answerB = Math.toDegrees(Math.acos((c*c+a*a-b*b) / (2*c*a)));
            double answerC = Math.toDegrees(Math.acos((b*b+a*a-c*c) / (2*b*a)));
                System.out.println("A: " + answerA);
                System.out.println("B: " + answerB);
                System.out.println("C: " + answerC);

    }else if(askingSSS == 'n'){
        System.out.println("Are you working with SAS?[y/n]");
        char askingSAS =keyboard.next().charAt(0);
        System.out.println("Please enter the 2 sides and 1 angle:");
        char twoSideOneAngle =keyboard.next().charAt(0);
        if(askingSAS == 'y'){
            System.out.println("Please enter an angle for a:");
            double a = keyboard.nextDouble();
            System.out.println("Please enter a side value for b:");
            double b = keyboard.nextDouble();
            System.out.println("Please enter a side value for c:");
            double c = keyboard.nextDouble();
                double answerA = Math.cos(Math.toDegrees(b*b+c*c-2*b*c)*(a));
                double answerB = Math.sin(Math.toDegrees(sin b/b = sin a/a));
                double answerC =  (b*b+a*a-c*c) / (2*b*a);  
                    System.out.println("A: " + answerA);
                    System.out.println("B: " + answerB);
                    System.out.println("C: " + answerC);
            }
        }
}

最佳答案

看起来您将所有内容都放在了 cos 和 sin 中。首先简化方程。

a² = b² + c² - 2bc cosA 变为 a = (b² + c² - 2bc cosA)^(1/2)。然后你可以从内到外工作。

在伪代码中:

answer = cos(A)
answer = answer * 2 * b * c
answer += b*b
answer += c*c
answer = sqrt(answer)

类似地,如果您在 sinB/b = sinA/a 中寻找 B,则这将变为 B = arcsin(b*sinA/a):

answer = sin(A)
answer = b * answer
answer = answer/a
answer = arcsin(answer)

您可能还想回顾一下您的一些逻辑。

else if(askingSSS == 'n'){
    System.out.println("Are you working with SAS?[y/n]");
    char askingSAS =keyboard.next().charAt(0);
    if(askingSAS == 'y'){
        // I change code here
        System.out.println("Please enter the 2 sides and 1 angle:");
        char twoSideOneAngle =keyboard.next().charAt(0);
        System.out.println("Please enter an angle for a:");
        double a = keyboard.nextDouble();
        System.out.println("Please enter a side value for b:");
        double b = keyboard.nextDouble();
        System.out.println("Please enter a side value for c:");
        double c = keyboard.nextDouble();
            double answerA = Math.cos(Math.toDegrees(b*b+c*c-2*b*c)*(a));
            double answerB = Math.sin(Math.toDegrees(sin b/b = sin a/a));
            double answerC =  (b*b+a*a-c*c) / (2*b*a);  
                System.out.println("A: " + answerA);
                System.out.println("B: " + answerB);
                System.out.println("C: " + answerC);
        }
    }

这会更有意义,因为您正在检查的角色现在是回答相关问题的角色。

关于Java- 在公式方面需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19128462/

相关文章:

java - Java 中字符串的 'new' 运算符有什么用?

java - 相互检查文件是否有 "fraud"

algorithm - 如何将 "snap"方向(2D)矢量指向罗盘(N、NE、E、SE、S、SW、W、NW)?

python - python中余弦的高效计算

java - 创建一个计算字符串中元音的数组

java - ActiveMQ 内存消耗通过屋顶(页面文件)...该怎么办?

java - PSelectionEventHandler 不触发回调

android - 使用 nextAfter(双开始,双向);在安卓系统中

c# - 返回符号的函数

c++ - 创建一个 Fast Sin() 函数来提高 fps ?快速 sin() 函数?