三角学:sin(x)得到负值

时间:2016-09-24 12:50:38

标签: python

我正在尝试解决作业:我需要编写一个程序,根据两个输入计算梯子的长度,即要达到的所需高度以及将梯子靠近墙壁所产生的角度

我使用以下公式将度数转换为弧度:

radians = (math.pi / 180) * x # x is the given angle by the user.

我也导入了数学库以使用它的功能。

def main():

    import math 

    print("this program calculates the length of a ladder after you give the height and the angle")

    h = eval(input("enter the height you want to reach using the ladder"))
    x = eval(input("enter the angle which will be created be leaning the ladder to the wall"))

    radians = ( math.pi / 180 ) * x

    length = h / math.sin(x)

    print("the length is:", length)


main()

我究竟做错了什么? 我知道代码遗漏了一些东西,如果有人可以帮助我填补空白,我会很感激。

3 个答案:

答案 0 :(得分:1)

计算后你从未使用radians

即。 length = h / math.sin(radians)

答案 1 :(得分:0)

要让crickt_007的答案绝对清楚:radians你计算之后没用过的length = h / math.sin(radians) 应该是正弦的参数:

package management;

import javax.swing.*;
import java.awt.*;

public class Library extends JFrame {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JLabel label1 = new JLabel();
    JLabel label3 = new JLabel();
    JLabel label2 = new JLabel();

    Library() {
        super("WELCOME TO KCA LIBRARY");

        setLayout(new FlowLayout());
        setBounds(500,500,500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container con = this.getContentPane();
        con.add(panel);

        panel.setLayout(null);

        label1=new JLabel("USERNAME");
        label1.setToolTipText("Enter Your Username");

        label2=new JLabel("REGISTRATION NUMBER");
        label2.setToolTipText("Enter Your Registration no");
        label2.setVerticalTextPosition(JLabel.BOTTOM);

        label3 = new JLabel("PASSWORD");
        label3.setToolTipText("Enter Your Password");

        add(label1);
        add(label2);
        add(label3);

        setVisible(true);  
    }

    public static void main(String[]args) {
        new Library();
    }
}

答案 2 :(得分:0)

你计算radians,没关系,但问题是你从未使用radians值。我认为你的代码必须改变如下:)

def main():

    import math 

    print("this program calculates the length of a ladder after you give the height and the angle")

    h = eval(input("enter the height you want to reach using the ladder"))
    x = eval(input("enter the angle which will be created be leaning the ladder to the wall"))

    radians = ( math.pi / 180 ) * x

    length = h / math.sin(radians)

    print("the length is:", length)


main()

如果您的输入都是5,则输出为the length is: 57.36856622834928