如何将变量转换为度?

时间:2013-10-10 13:35:04

标签: java math casting 51degrees

我正在尝试将变量tri2转换为降级,方法是将其包含在toDegree()中并将其设置为名为triDegree的新变量。但是当我转换变量并运行应用程序时,我在logcat中得到以下内容:http://pastebin.com/HKDSAuQK

应用程序运行正常并计算到我将其转换为度数的点。任何人都可以在这里看到我的实施有什么问题吗?

public void onClick(View v) {
    // TODO Auto-generated method stub
    try {
        String getoffsetlength = data.offsetLength.getText().toString(); 
        String getoffsetdepth = data.offsetDepth.getText().toString(); 
        String getductdepth = data.ductDepth.getText().toString(); 

        double tri1,tri2;
        double marking1,marking2;
        double triDegree;

        double off1 = Double.parseDouble(getoffsetlength);//length
        double off2 = Double.parseDouble(getoffsetdepth);//depth
        double off3 = Double.parseDouble(getductdepth);//duct depth

        marking1 = Math.sqrt(Math.pow(off1,2) + Math.pow(off2,2));
        tri1 = Math.atan(off2 / off1);
        tri2 = (180 - tri1) / 2;
        triDegree = Math.toDegrees(tri2);
        marking2 = off3 / Math.tan(triDegree);

        Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
        myIntent.putExtra("number1", marking1);
        myIntent.putExtra("number2", marking2);
        startActivity(myIntent);
        //make a toast 
        Toast.makeText( getBaseContext(), "Calculating!", Toast.LENGTH_SHORT
             ).show(); 
    }
    catch (NumberFormatException e) {
        // TODO: handle exception
        System.out.println("Must enter a numeric value!");
    }
}

1 个答案:

答案 0 :(得分:2)

错误发生在这里:

tri2 = (180 - tri1) / 2;

此时,您会在转化前将tri1视为度数。如果要从“直线”180°角的值中减去一个角度,请使用π而不是180,弧度中直角的度量

tri2 = (Math.PI - tri1) / 2;

此调用也不正确

triDegree = Math.toDegrees(tri2);
marking2 = off3 / Math.tan(triDegree);

因为triDegree以度数表示,而tan期望以弧度表示的值。