无限双运算产生无限java/android studio

14ifxucb  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(399)

我试着除法,结果是无穷大。有人能帮我解释一下为什么会这样吗?
我搜索了一些论坛,但没有找到答案。
我有这样的想法:

public class CalculoSapIsolada extends AppCompatActivity
{
    double areaAcoX;
    double numeroBarrasX;
    double areaBarraX;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculo_sap_isolada);

        //Here is where i get the value of areaBarraX

        if(SapataIsolada.getAço()==50)
        {
            final Spinner staticSpinner = (Spinner) findViewById(R.id.spinner_diametro_aço_x);
            ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(this, R.array.diametro_aço_CA_50, android.R.layout.simple_spinner_item);
            staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            staticSpinner.setAdapter(staticAdapter);
            staticSpinner.setPrompt("Selecione o diâmetro da armadura");
            staticSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
            {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) 
                {
                    Object item = adapterView.getItemAtPosition(i);
                    if (i == 0) {
                    diametroAçoX = 0.5;
                    areaBarraX = 0.196;
                    }
                    if (i == 1) {
                    diametroAçoX = 0.63;
                    areaBarraX = 0.31;
                    }
                    if (i == 2) {
                    diametroAçoX = 0.8;
                    areaBarraX = 0.5;
                    }
                    if (i == 3) {
                    diametroAçoX = 1;
                    areaBarraX = 0.785;
                    }
                    if (i == 4) {
                    diametroAçoX = 1.25;
                    areaBarraX = 1.22;
                    }
                    if (i == 5) {
                    diametroAçoX = 1.6;
                    areaBarraX = 2.01;
                    }
                    if (i == 6) {
                    diametroAçoX = 2;
                    areaBarraX = 3.14;
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
           });
        }
    /* the value of areaAcoX is calculated previously and during the testrun it receives areaAcoX=11.36016 and the value of areaBarraX=0.5 */
    numeroBarrasX=areaAcoX/areaBarraX;

        Button calcular =(Button)findViewById(R.id.calcular_resultado);
        calcular.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                //in altura.setText, areaAcoX==11.36016

                TextView altura = (TextView)findViewById(R.id.alturaTotal);
                altura.setText(""+areaAcoX);

                //In ladoLtopo.setText, areaBarraX==0.5

                TextView ladoLtopo = (TextView)findViewById(R.id.ladoL_topo);
                ladoLtopo.setText(""+areaBarraX);

                //In numBarrasX.setText, numeroBarrasX==Infinity

                TextView numBarrasX = (TextView)findViewById(R.id.numeroBarras_x);
                numBarrasX.setText(""+numeroBarrasX);

        });

    }
}

我在这里没有显示的代码中有一些类似的操作,它们的值中没有无穷大的错误。

uz75evzq

uz75evzq1#

用0.0除以类型double得到无穷大。也许这一行之前有一个除以0.0的值 areaBarraX 已初始化:

numeroBarrasX=areaAcoX/areaBarraX;

相关问题