Java-需要公式方面的帮助

时间:2013-10-02 01:08:06

标签: java math sin jcreator cos

我最近在这里发布了,但我又需要帮助(我很新)我得到了第一部分(SSS),但是我需要帮助的第二部分,我不明白如何把a2 = 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);
            }
        }
}

}

2 个答案:

答案 0 :(得分:2)

看起来你把所有东西都放在了cos和罪中。首先简化方程式。

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);
        }
    }

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

答案 1 :(得分:1)

double answerB = Math.sin(Math.toDegrees(sin b/b = sin a/a));

Math.toDegrees在技术上应该在外面:

double answerB = Math.toDegrees(Math.sin(sin b/b = sin a/a));

同样罪恶b / b与1相同......所以我认为你的等式没有意义......也许是罪(b)/ b ....

double answerB = a*(Math.sin(b)/Math.sin(a));

试试看,看看它是否有效。

最新编辑:代码说sin b / b,我误解了。代码应该是:

double answerB = answerA*(Math.sin(Math.toRadians(b))/Math.sin(Math.toRadians(a)));

我认为“answerA”与A(侧面)相同。