用java计算三角形的邻边?

发布于 2025-01-02 18:32:21 字数 1499 浏览 1 评论 0原文

我正在开发一个应用程序,可以计算梯子距离墙壁的距离,在这种情况下,梯子是斜边,其值将根据用户输入而变化,因此将以英尺为单位计算,X = 73 度, A = cos 73 相邻/斜边。检查下面的图像以更好地理解:

在此处输入图像描述

我做了一些研究,发现了梯级之间的距离梯子大约 1 英尺,用户必须计算梯级数量并将该数字输入到 EditText 中,然后按下按钮计算结果,同时使用 Toast 显示结果。 那么我如何能够使用java中的三角函数来计算A以及我如何能够将字符串从EditText转换为双精度请帮我弄清楚我正在这里从事一个学校项目是到目前为止,我的代码,请告诉我必须添加或删除哪些内容,谢谢您的宝贵时间!
另外,我已手动将 A 转换为弧度,因为我也不知道如何将度数转换为弧度!

package com.ladder;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast;  
import java.lang.Math;

public class LadderActivity extends Activity {  double x;
    double r = Math.cos(1.274090354);

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

            EditText edt1 = (EditText) findViewById(R.id.et1);
            String k = edt1.toString();


        Button btn = (Button) findViewById(R.id.bt1);
         btn.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                Toast.makeText(getApplicationContext(), " FT", Toast.LENGTH_SHORT).show();

                // TODO Auto-generated method stub
                            }       });
    } }

I am working on an application that can calculate how far a ladder has to be from the wall in this case the ladder is the hypotenuse and the value of that will vary on the users input so it will be calculated in feet , X = 73 degrees, A = cos 73 Adjacent/Hypotenuse. Check the bellow image to understand better:

enter image description here

I did some research and I found out the distance between the rungs on a ladder are about 1 feet, the user would have to count the number of rungs and input that number to the EditText and press the button to calculate the results while displaying it using Toast.
SO how would I be able to calculate A using trigonometric functions in java and how would I be able to convert the strings from EditText to a double please help me figure this out am working on a school project here is my code so far, please tell me what I have to add or remove thank you for your time kind of new to this!
Also I have manually converted A to radians since I do not know how to convert degrees to radians also!

package com.ladder;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast;  
import java.lang.Math;

public class LadderActivity extends Activity {  double x;
    double r = Math.cos(1.274090354);

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

            EditText edt1 = (EditText) findViewById(R.id.et1);
            String k = edt1.toString();


        Button btn = (Button) findViewById(R.id.bt1);
         btn.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                Toast.makeText(getApplicationContext(), " FT", Toast.LENGTH_SHORT).show();

                // TODO Auto-generated method stub
                            }       });
    } }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2025-01-09 18:32:21

您可以使用java的Math类,下面是您问题的详细答案
将字符串从 Edittext 转换为双精度

    //Your editText
    EditText editText = null;
    //Convert editText value to double.
    String stringValue = editText.getText().toString();
    Double doubleValue = 0.0;
    try{
        doubleValue = Double.parseDouble(stringValue);
    }catch(NumberFormatException e){
        e.printStackTrace();
    }

如何将度数转换为弧度

        double angleInDegrees = 189.0;// angle in degree
        double angleInRadians = angleInDegrees * Math.PI / 180.0;

这些是三角形的边

double Hypotenuse, Opposite, Adjacent;

计算“相邻”值。

        double angleInDegrees = (double)73;// angle in degree
        double angleInRadians = angleInDegrees * Math.PI / 180.0;

        double Adjacent = Math.cos(angleInRadians) * Hypotenuse          
        OR
        double Adjacent = Math.tan(angleInRadians)* Opposite  

计算相反值

        Opposite = Math.sin(angleInRadians)* Hypotenuse;
        Opposite = Math.tan(angleInRadians)*Adjacent;

计算斜边值

        Hypotenuse = Opposite/Math.sin(angleInRadians);
        Hypotenuse = Adjacent/Math.cos(angleInRadians);

You can use Math class of java and below is detailed answer of your question
Convert string from Edittext to double

    //Your editText
    EditText editText = null;
    //Convert editText value to double.
    String stringValue = editText.getText().toString();
    Double doubleValue = 0.0;
    try{
        doubleValue = Double.parseDouble(stringValue);
    }catch(NumberFormatException e){
        e.printStackTrace();
    }

How to convert Degree into radian

        double angleInDegrees = 189.0;// angle in degree
        double angleInRadians = angleInDegrees * Math.PI / 180.0;

These are sides of triangle

double Hypotenuse, Opposite, Adjacent;

Calculate 'Adjacent' Value.

        double angleInDegrees = (double)73;// angle in degree
        double angleInRadians = angleInDegrees * Math.PI / 180.0;

        double Adjacent = Math.cos(angleInRadians) * Hypotenuse          
        OR
        double Adjacent = Math.tan(angleInRadians)* Opposite  

Calculate Opposite Value

        Opposite = Math.sin(angleInRadians)* Hypotenuse;
        Opposite = Math.tan(angleInRadians)*Adjacent;

Calculate Hypotenuse Value

        Hypotenuse = Opposite/Math.sin(angleInRadians);
        Hypotenuse = Adjacent/Math.cos(angleInRadians);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文