0

我编写了这个简单的应用程序来对用户输入进行单独计算。这是我的计算类,其中计算传递给两个变量:

public void onClick(View v) {
        // TODO Auto-generated method stub

        try {

            String getoffsetlength = offsetLength.getText().toString(); 
            String getoffsetdepth = offsetDepth.getText().toString(); 
            String getductdepth = ductDepth.getText().toString(); 

            double tri1,tri2;
            double marking1,marking2;

            double off1 = Double.parseDouble(getoffsetlength);
            double off2 = Double.parseDouble(getoffsetdepth);
            double off3 = Double.parseDouble(getductdepth)
                    ;
            marking1 = Math.pow(off1,2) + Math.pow(off2,2);
            tri1 = (float)off2/(float)off1;
            tri2 = (float)off3/Math.atan((float)tri1);
            marking2 = (float)off3/Math.atan(tri2);


            Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
            myIntent.putExtra("number1", marking1);
            myIntent.putExtra("number2", marking2);
            startActivity(myIntent);


        } catch (NumberFormatException e) {
            // TODO: handle exception
            System.out.println("Must enter a numeric value!");

        }

    }

在我的计算结果类中,我将结果转换为双倍,它们在文本框中表示。我想知道转换结果的另一种方法,因为它们似乎是浮点数或计算已关闭。

        Intent intent = getIntent();
        double mark1 = intent.getDoubleExtra("number1", 0);
        double mark2 = intent.getDoubleExtra("number2", 0);

        //set the variables on EditTexts like this :

        result1 = (EditText)findViewById(R.id.mark1);
        result2 = (EditText)findViewById(R.id.mark2);
        result1.setText(mark1+"");
        result2.setText(mark2+"");

结果计算不是我所期望的:

1.我输入的内容:

https://plus.google.com/112628117356947034778/posts/En1Bueexoc7

2.结果计算:

https://plus.google.com/112628117356947034778/posts/ApqinYrp8Na

4

1 回答 1

1

当您使用以下方法进行计算时,您将所有双打都转换为浮点数(float)

        tri1 = (float)off2/(float)off1;
        tri2 = (float)off3/Math.atan((float)tri1);
        marking2 = (float)off3/Math.atan(tri2);

从你的代码中删除所有这些,那么你只需要处理双打。

        tri1 = off2 / off1;
        tri2 = off3 / Math.atan(tri1);
        marking2 = off3 / Math.atan(tri2);

希望这能解决问题!:)

于 2013-09-29T20:45:18.177 回答